SQL 2005 Selecting data Into a #TempTable then query it

  • I have the following...

    Declare @TempTable As VarChar(200)

    Set @TempTable = 'UserName200951125126427'

    EXEC('Select * Into #' + @TempTable From [MySourceTable])

    I get the result...

    (253 row(s) affected)

    I try and query the temp table and I get the...

    Invalid object name

    If data was inserted, then where/what table then?

  • Warren Peace (5/1/2009)


    I have the following...

    Declare @TempTable As VarChar(200)

    Set @TempTable = 'UserName200951125126427'

    EXEC('Select * Into #' + @TempTable From [MySourceTable])

    I get the result...

    (253 row(s) affected)

    I try and query the temp table and I get the...

    Invalid object name

    If data was inserted, then where/what table then?

    You are using dynamic SQL. The temporary table is created within the process of the EXEC. When that returns, the temporary table goes out of scope and is dropped.

  • That was it.

    Thanks, I got it going!

  • Warren Peace (5/1/2009)


    That was it.

    Thanks, I got it going!

    Could you post the code that solved the problem, please? Thanks.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.
    "Change is inevitable... change for the better is not".

    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)
    Intro to Tally Tables and Functions

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

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