Local vs Global Temp tables

  • Hi all,

    Can anyone shed any light on this please.

    I am creating a local temporary table dynamically, building it up in a cursor. Once finished I execute the created temp table definition string via the command:

    exec (@end_table);

    However I cannot access it for insertion, i get the error:

    Server: Msg 208, Level 16, State 1, Line 1

    Invalid object name '#JimbobsTemptable'.

    However if I change the local temp table to a global one via (# -> ##) it seems to work.

    Is this because the local table's scope was within the execute statement?

    Any help gratefully received.

    Thanks.

    Rob.

  • Yes that is correct. The temp table lives in the scope. You could build the rest of you code into the String to run thru the execute statement thou so all occurr in the same scope. Global exist in all scopes and thus is accessible by all connections until the last connection referencing it stops.

  • Thanks very much for that.

    Very good idea about inclusion in the exec statement!

    Regards,

    Rob.

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply