Using RedGate's SQL Prompt & loving it. But I used the "curff" snippet for the first time today ("Fast-forward read-only cursor"), and it produced the following TSQL (which I've filled in to make it a concrete example):
DECLARE @variable INT DECLARE Curse CURSOR FAST_FORWARD READ_ONLY FOR SELECT ID FROM #TempTable OPEN Curse FETCH NEXT FROM Curse INTO @variable WHILE @@FETCH_STATUS = 0 BEGIN FETCH NEXT FROM Curse INTO @variable --This seems wrong... PRINT ('Hello, I am # ' + CAST(@variable AS VARCHAR(10))) END CLOSE Curse DEALLOCATE Curse
Isn't the location of the inner
FETCH NEXT (inside the
WHILE loop) incorrect? It should be at the end of the loop, or at least after the query (queries) that need to use the variable, no?