Where is the memory allocated for the T-SQL variable?

In T-SQL, when we do something like

declare @PolicyId int = 20

4 bytes are allocated, since the size intis 4 bytes, but where is this memory allocated - is it on the stack or heap?

+3
source share
1 answer

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...).

+6

All Articles