I have a cursor that goes through a list of table+column names (ordered by table and column name) and produces SQL queries. Part of a query is constructed on each cycle and the whole query is executed when the table name is different from previous cycle's table name (which is saved in a variable).
The problem with this approach is that last row does not trigger the execution part, as there is no new table name after the last row. I have to repeat the whole code for the logic after the loop just to handle the last row and execute last query.
Loop condition is the usual:
WHILE @@FETCH_STATUS = 0 ... <logic> ... <fetch next> END
WHILE 1=1 ... <logic> ... IF @@FETCH_STATUS = -1 BREAK ... <fetch next> END
I think the logic you're looking for is:
Declare YourCursor Cursor ... ... While (1 = 1) Begin Fetch Next From YourCursor Into @C_YourVariables If @@Fetch_Status <> 0 Break <Do stuff> End Close YourCursor Deallocate YourCursor