Hi:
Could any one tell me why the loop doesn't stop running, even though the count exceeds the condition - here I am trying to insert data in batches . Thanks
DECLARE @batch_size INT,
@row_counter INT,
@maxrow_count INT;
SET @batch_size = 5000; -- batch of 5000
SET @row_counter= 1;
SELECT @maxrow_count = max(RNO) FROM #DATA
PRINT @maxrow_count
WHILE @row_counter <= @maxrow_count
BEGIN
INSERT INTO DBO.PrevPriceCheck (SecurityID,Ticker,Cusip ,Price_Date,Price_final,PrvPriceDate )
SELECT a.securityid,a.ticker,
a.cusip,a.price_date
,a.price_final,MAX(b.PriceDate) PrvPriceDate --into DBO.PrevPriceCheck
FROM #DATA a WITH(NOLOCK)
LEFT JOIN ad_08142015 b WITH(NOLOCK)ON a.SecurityId= b.SecurityID
and a.price_date > b.PriceDate
and b.DataId = 7
AND A.RNO between @row_counter and (@row_counter + @batch_size)
GROUP BY a.ticker,a.cusip,a.price_date,a.price_final,a.securityid
-- Set the @row_counter to the next batch start
SET @row_counter = @row_counter + @batch_size + 1;
END
Add PRINT @row_counter after the SET (before the END) to see what value it gets up to. Otherwise, please format your code so that we can copy/paste. Right now some of it is in a code block and some of it isn't.
Hi;
Hmm I tried tried that , and the counter gets incremented after a long time and the data gets inserted twice... Is there any mistake in the script ? Thanks
The below is the O/P message that i get .....It goes on inserting.....The Row count in #Data is 522423 ...and it seems that the row counter is exceeding 5000 -the batch size-
1
Warning: Null value is eliminated by an aggregate or other SET operation.
(522423 row(s) affected)
5001
Warning: Null value is eliminated by an aggregate or other SET operation.
(522423 row(s) affected)
10001
Warning: Null value is eliminated by an aggregate or other SET operation.
(522423 row(s) affected)
15001
Warning: Null value is eliminated by an aggregate or other SET operation.
(522423 row(s) affected)
20001
Warning: Null value is eliminated by an aggregate or other SET operation.
(522423 row(s) affected)
25001
Warning: Null value is eliminated by an aggregate or other SET operation.
(522423 row(s) affected)
30001
Warning: Null value is eliminated by an aggregate or other SET operation.
(522423 row(s) affected)
35001
Warning: Null value is eliminated by an aggregate or other SET operation.
(522423 row(s) affected)
40001
Warning: Null value is eliminated by an aggregate or other SET operation.
(522423 row(s) affected)
45001
Warning: Null value is eliminated by an aggregate or other SET operation.
(522423 row(s) affected)
50001
Warning: Null value is eliminated by an aggregate or other SET operation.
(522423 row(s) affected)
55001
Warning: Null value is eliminated by an aggregate or other SET operation.
(522423 row(s) affected)
60001
Warning: Null value is eliminated by an aggregate or other SET operation.
The problem with additional conditions involved in OUTER JOIN
There is a difference(result set) between adding additional conditions in ON clause of LEFT OUTER JOIN
and where clause. If you want to restrict to only batch size (5000) keep below condition in where clause
WHERE A.RNO between @row_counter and (@row_counter + @batch_size)