declare @counter integer = 0 while @counter < 10 begin declare @tableVar table (code integer) -- to test, comment out this line delete @tableVar -- need to clear otherwise previous results will accumulate insert into @tableVar values (@counter) select * from @tableVar set @counter = @counter + 1 end
Friday, 19 August 2011
Using Table Variable Inside a Loop
Be careful when using a table variable inside a loop. If it's not cleared, the rows inside the table variable might accumulate after each iteration. It seems that in T-SQL, a variable declared inside a loop is not automatically re-initialised.
Labels:
SQL
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment