Nested Temporary Tables

  • SQLkiwi (12/20/2010)


    Both have their strengths and weaknesses, and the more rounded approach is to use each where it is suited. Table variables have many advantages aside from passing TVPs around:

    1. Named constraints are problematic with #temp tables (another name-collision problem). You cannot name a constraint on a table variable, so again the problem is avoided in all cases.

    2. Table variables can use user-defined data types and XML schemas defined in the context database. Temporary tables cannot use either, unless they happen to have identical definitions in tempdb, which is inconvenient, and tough to maintain robustly.

    3. Temporary tables inherit the collation of tempdb, whereas table variables use the context database. It is not all that uncommon for user databases to differ from tempdb in collation, and I do not enjoy resolving collation conflicts.

    FYI, if this is a "contained database" (in SQL 11/Denali), then the temporary table will inherit collation from the current database, and cannot use UDDTs/XML schema/UDFs at all. (BOL Reference

    4. Data stored in a table variable in the context of a user transaction is not rolled back with the transaction. This can be invaluable, e.g. where we need to log information after a roll back.

    5. Table variables are the only available choice in function definitions.

    6. Table variables are the only available choice to pass a table to a procedure.

    So, I am absolutely not saying that table variables should replace temporary tables every time. I personally prefer to start with a table variable design, and look for reasons that would justify changing to use a temporary table.

    In practice, I often find that the presence of a temporary object (of sufficient size or complexity to make a table variable a poor choice) an indicator that I am doing something dumb. Specifically, manipulating large amounts of data, creating indexes, relying on statistics to produce a non-trivial plan...all this is work that is performed again and again, on every execution. Often, it indicates that the present overall database design is lacking.

    Paul

    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

  • WayneS (12/20/2010)


    6. Table variables are the only available choice to pass a table to a procedure.

    I know XML is not realy a table, but it can be used to pass a table to a procedure as an input parameter. :w00t:

  • SanDroid (12/20/2010)


    WayneS (12/20/2010)


    6. Table variables are the only available choice to pass a table to a procedure.

    I know XML is not realy a table, but it can be used to pass a table to a procedure as an input parameter. :w00t:

    Yep, prior to being able to pass a table to a procedure, one was limited to using XML or delimited strings. I haven't actually played with passing a table yet, but it does look really promising!

    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

  • WayneS (12/20/2010)


    SanDroid (12/20/2010)


    WayneS (12/20/2010)


    6. Table variables are the only available choice to pass a table to a procedure.

    I know XML is not realy a table, but it can be used to pass a table to a procedure as an input parameter. :w00t:

    Yep, prior to being able to pass a table to a procedure, one was limited to using XML or delimited strings. I haven't actually played with passing a table yet, but it does look really promising!

    It's relatively simplistic. Create a data type of the table, then make the paramete to the stored proc of the same type. Declare a table variable of the same type, insert a bunch of stuff and then pass it like you would anything else.

    We needed it because of a generalized search stored proc where collected sets of search criteria were applied in an OR fashion... it worked splendidly.



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

  • It gives an error......

    Error: "Column name or number of supplied values does not match table definition

    Thanks & Regards, Kartik M Kumar..

Viewing 5 posts - 61 through 64 (of 64 total)

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