keeping a table variable alive

  • Hi,

    I'm following some tutortials at the moment that ask me to create @table variables. I find that I have to run the entire script over and over again to get my results as the table variable is only alive when running the scripts.

    Is there an enviroment or session variable I can set that will keep the variable alive during my session without having to include in my script every time?

    Any help welcome on this.

    thanks,

    EamonOD:-)

  • No, this is the way that table variables are supposed to work.

    What you can do is change all of the "DECLARE @TABLE TABLE" statements to "CREATE TABLE #TABLE", then change @TABLE to #TABLE everywhere in your code. It will then persist until explicitly dropped, or the connection is closed.

    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

  • Or plan B if you'd rather not wait... just add this where you need to debug

    SELECT * FROM #tmp from @table

  • Thanks alot guys this is what I thought but decided to post this question just in case I missed some new feature addition to SQL server.

    Thanks for the help on this.

    EamonOD

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

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