Home Forums SQL Server 2005 Development Create Temporary Tables Dynamically in cursor loop RE: Create Temporary Tables Dynamically in cursor loop

  • Good question! I do not know how beause of scoping problems. If you execute:

    exec ('create table #what(id int)')

    select * from #what

    /* -------- Sample Output: --------

    Msg 208, Level 16, State 0, Line 3

    Invalid object name '#what'.

    */

    The "#what" table never gets created within the same scope as the SELECT statement. You can change "#what" to "##what" but temp tables that start with ## are global temp tables rather than local temp tables. Use of global temp tables can have dangerous side affects.