I want to set DriverID column of Driver Table to 5000, 5001 ,....
For this purpose I write this script :
use WeighbridgeDB
GO
DECLARE @NewDriverID int;
SET @NewDriverID = 5000;
DECLARE Driver_Cursor CURSOR FOR
SELECT DriverID FROM Driver
FOR UPDATE;
OPEN Driver_Cursor;
FETCH NEXT FROM Driver_Cursor
WHILE @@FETCH_STATUS = 0
BEGIN
UPDATE Driver SET DriverID = @NewDriverID WHERE CURRENT OF Driver_Cursor;
SET @NewDriverID += 1;
FETCH NEXT FROM Driver_Cursor
END
CLOSE Driver_Cursor;
DEALLOCATE Driver_Cursor;
GO
But the While loop doesn't stop and the value of @@FETCH_STATUS is always 0. I think the Cursor rebuild itself since update occur on table or something.
How to correct this situation?
thanks.
INTO. It means that the row will be sent to the client of the connection instead. And since the OP isn't trying to use any value from the current row, it's not obviously wrong.