I have a table named Blacklist
and table named Order
.
Both have CustomerId
column.
Stored Procedure ExecOrder
manipulates Order
table.
My goal is to exclude Orders that have Blacklisted CustomerId (meaning : Order’s CustomerId is in Blacklist table).
I edited ExecOrder
SP like this:
DECLARE @Temp TABLE ( CustomerId UNIQUEIDENTIFIER ); INSERT INTO @Temp SELECT RightSide FROM Blacklist WHERE LeftSide = @CustomerId; BEGIN DECLARE db_cursor CURSOR FOR SELECT OrderId FROM dbo.[Order] ord LEFT OUTER JOIN @Temp t ON t.CustomerId <> ord.CustomerId AND ord.CustomerId <> @CustomerId WHERE AssetId = @BaseAsset AND CurrencyId = @QuoteAsset AND OrderTypeId <> @OrderType AND [QuotePrice] <= @QuotePrice AND OrderStatusId = 10 AND Amount > ISNULL(AmountFilled, 0) AND OrderId < @OrderId AND OrderBookId = @OrderBookId AND DeliveryStart = @DeliveryPeriodStart AND DeliveryEnd = @DeliveryPeriodEnd AND @MinAmount <= Amount - ISNULL(AmountFilled, 0) ORDER BY OrderDate; END;
@Temp table returns correct list of CustomerIds. Problem is that blacklisted orders are not excluded.
Advertisement
Answer
Your @Temp table is holding blacklisted customers.
-- contains blacklisted customer INSERT INTO @Temp SELECT RightSide FROM Blacklist WHERE LeftSide = @CustomerId;
Now, you need to select orders of customers not existing in @Temp table.
-- You need to select orders, where customerId not exists in @Temp table SELECT OrderId FROM dbo.[Order] ord WHERE NOT EXISTS(SELECT 1 from @Temp WHERE customerId = ord.CustomerId) ...