inserting into temproray table

  • I am inserting into temprary table using INTO statement by execute sqlstring.

    e.g

    Declare @sqlstring as nvarchar(max)

    set @sqlstring =" Select column1,column2 INTO #temptable From TableName"

    executesql @sqlstring

    Select * from #temptable

    but it gives be error .....#temptable is undefined.

    What is the problem??

    Please help its urgent.......................

  • it's scope; the table exists INSIDE the exec statement, but is destroyed when it comes back to the "outer" scope of code that called the EXEC..

    you have to create the temp table before using the exec statement;

    CREATE TABLE #temptable(Column1 varchar(30), etc....)

    set @sqlstring =" INSERT INTO #temptable Select column1,column2 From TableName"

    executesql @sqlstring

    i assume this is an example, as nothing you really posted needs to be done with dynamic SQL;

    Lowell


    --help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

  • Hey Lowell... congrats on the 5K mark. You're one of those that works for every single post and your 5K mark is like a 10K mark for others on other forums. Well done ol' friend.

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

  • thanks lowell

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

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