• A little more info on declaring variables (scalar and table variables) - whilst the example in this question is quite right and the loop does not attempt to create another copy of the table variable, you must declare variables before you attempt to use them

    If the code is adjusted so to be as follows...

    set @I= 1

    DECLARE @i INT

    WHILE @i < 5

    BEGIN

    SET @i = @i + 1

    INSERT INTO @j-2

    SELECT @i

    END

    DECLARE @j-2 AS TABLE

    (

    i INT

    )

    SELECT * FROM @j-2

    you end up with 2 errors because there is an attempt to assign a value to @i before it is declared and an attempt to insert into @j-2 before it is declared.