I have a doubt regarding the variable declaration in a nested cursors scenario.
This is an small nested cursor sample that i found. In other samples I've seen I also find DECLARE clauses inside the first cursor.
DECLARE @ClientID int;
DECLARE Cur1 CURSOR FOR SELECT ClientID From Folder;
OPEN Cur1
FETCH NEXT FROM Cur1 INTO @ClientID;
SELECT @FETCH_Cur1 = @@FETCH_STATUS
WHILE @FETCH_Cur1 = 0
BEGIN
DECLARE @UID int;
DECLARE Cur2 CURSOR FOR SELECT UID FROM Attend Where ClientID=@ClientID;
OPEN Cur2;
FETCH NEXT FROM Cur2 INTO @UID;
SELECT @FETCH_Cur2 = @@FETCH_STATUS
WHILE @FETCH_Cur2 = 0
BEGIN
PRINT 'Found UID: ' + Cast(@UID as Varchar);
FETCH NEXT FROM Cur2 INTO @UID;
SELECT @FETCH_Cur2 = @@FETCH_STATUS
END;
CLOSE Cur2;
DEALLOCATE Cur2;
FETCH NEXT FROM Cur1 INTO @ClientID;
SELECT @FETCH_Cur1 = @@FETCH_STATUS
END;
PRINT 'DONE';
CLOSE Cur1;
DEALLOCATE Cur1;
The code works, but my doubt is if it's correct the DECLARATIONS inside the first cursor.
DECLARE @UID int;
Shouldn't Declarations be placed at the beginning of code, as is normally done for other programming languages?
You can
DECLARE
a variable inside aWHILE
, yes; the latterDECLARE
s will simply be ignored. If you declared the variable and assigned it a value at the time (for exampleDECLARE @UID int = 1;
it would be assigned1
in each iteration:DB<>fiddle
Of course, I personally prefer to
DECLARE
the variables outside of theWHILE
as I feel the code is "cleaner", but that doesn't mean you have to.