T-SQL variables are not on the stack or on the heap. They are called logical entities in the current execution context. Placing an advertisement inside a loop is the same as placing it outside the loop. You will not cause memory depletion or stack overflow by placing variables inside a loop. See the following code:
declare @i int =0
while @i<2
begin
declare @x int = 0;
set @x += 1
print @x;
set @i += 1;
end
print @x;
, @x , . . . (, 1, 1,2,3...).