Execs and temporary tables

  • That explanation requires expanding slightly, because there is more than one type of temporary table.

    Local temporary tables (prefixes with '#') go out of scope when the exec command completes. However, use '##' to create a Global temporary table and the table can be accessed outside of the exec statement that created it.

  • My gut instinct on this was right. Unfortunately I did not listen to this and chose incorrectly. Doh!!:hehe:

  • Good question, thanks.

  • BrainDonor (7/6/2012)


    That explanation requires expanding slightly, because there is more than one type of temporary table.

    Local temporary tables (prefixes with '#') go out of scope when the exec command completes. However, use '##' to create a Global temporary table and the table can be accessed outside of the exec statement that created it.

    Thanks for the amplification.

    And thanks to OP for the interesting question.

    [font="Verdana"]Please don't go. The drones need you. They look up to you.[/font]
    Connect to me on LinkedIn

  • Great question and a nice way to end the week. 🙂



    Everything is awesome!

  • This question made up for yesterday's beating. Glad we ending the week with this one.

  • David Harder (7/6/2012)


    This question made up for yesterday's beating. Glad we ending the week with this one.

    Yes, I agree.

  • Emphasis (bolding) added by this poster

    BrainDonor (7/6/2012)


    That explanation requires expanding slightly, because there is more than one type of temporary table.

    Local temporary tables (prefixes with '#') go out of scope when the exec command completes. However, use '##' to create a Global temporary table and the table can be accessed outside of the exec statement that created it.

    At the completion of the command given in the QOD

    exec ('create table #qotd2 (id int))' follows the above since the exec command completes, that is #qotd2 has gone out of scope.

    To illustrate, the code below has been modified so that the entire sequence of commands is contained within the scope of the exec command.

    exec ('create table #qotd2 (id int)

    alter table #qotd2 add i int

    insert into #qotd2 (i) values (2)

    select * from #qotd2

    drop table #qotd2')

    The above then will return for the SELECT statement the values:

    id i

    NULL 2

    If everything seems to be going well, you have obviously overlooked something.

    Ron

    Please help us, help you -before posting a question please read[/url]
    Before posting a performance problem please read[/url]

  • Always nice to have an easy one on Friday....



    --Mark Tassin
    MCITP - SQL Server DBA
    Proud member of the Anti-RBAR alliance.
    For help with Performance click this link[/url]
    For tips on how to post your problems[/url]

  • mtassin (7/6/2012)


    Always nice to have an easy one on Friday....

    Yes it is.:-D

    Jason...AKA CirqueDeSQLeil
    _______________________________________________
    I have given a name to my pain...MCM SQL Server, MVP
    SQL RNNR
    Posting Performance Based Questions - Gail Shaw[/url]
    Learn Extended Events

  • Nice question, thanks for the back to basics.

    Happy weekend, everyone.

    "El" Jerry.

    "El" Jerry.

    "A watt of Ottawa" - Gerardo Galvan

    To better understand your help request, please follow these best practices.[/url]

  • SQLRNNR (7/6/2012)


    mtassin (7/6/2012)


    Always nice to have an easy one on Friday....

    Yes it is.:-D

    Count me in. 🙂

  • Nice question to end the week on. Thanks!

  • Thanks for the question! but the set of possible answers was "too easy": once I saw that B#2 was going to error out while B#3 was not, I didn't look at the subsequent Bs :-).

  • mtassin (7/6/2012)


    Always nice to have an easy one on Friday....

    I concur... but what is easy for one person isn't for another.

    Wayne
    Microsoft Certified Master: SQL Server 2008
    Author - SQL Server T-SQL Recipes


    If you can't explain to another person how the code that you're copying from the internet works, then DON'T USE IT on a production system! After all, you will be the one supporting it!
    Links:
    For better assistance in answering your questions
    Performance Problems
    Common date/time routines
    Understanding and Using APPLY Part 1 & Part 2

Viewing 15 posts - 1 through 15 (of 31 total)

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