I don't understand the meaning of following lines:
WHILE 1 = 1
BEGIN
FETCH NEXT FROM SomeCursor INTO @SomeId, @SomeOtherColumn
IF @@FETCH_STATUS <> 0 BREAK
What are the meanings of while 1=1? and if fetch status is different than 0?
1=1 is just a short condition which always return true, that is, loop forever (well, until you break out of it elsewhere).
As for fetch status values, as usual MSDN is your friend here. From https://msdn.microsoft.com/en-us/library/ms187308.aspx
- 0 = The FETCH statement was successful.
- -1 = The FETCH statement failed or the row was beyond the result set.
- -2 = The row fetched is missing.
E.g. if you get anything other than 0, things have gone wrong, so there's no point in continuing.
On a side note, MSDN also notes that is is a legacy thing, @@FETCH_STATUS
is global, and therefore where multiple cursors are being used it is not to be trusted. Instead lookup your cursor's individual fetch status value from the sys.dm_exec_cursors
dynamic management function.