Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase «««34567

Nested Temporary Tables Expand / Collapse
Author
Message
Posted Monday, December 20, 2010 2:09 PM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 7:55 PM
Points: 6,582, Visits: 8,860
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
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, How to ask a question, Performance Problems, Common date/time routines,
CROSS-TABS and PIVOT tables Part 1 & Part 2, Using APPLY Part 1 & Part 2, Splitting Delimited Strings
Post #1037399
Posted Monday, December 20, 2010 2:22 PM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Thursday, January 31, 2013 8:01 AM
Points: 1,232, Visits: 1,046
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.
Post #1037406
Posted Monday, December 20, 2010 2:45 PM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 7:55 PM
Points: 6,582, Visits: 8,860
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.


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
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, How to ask a question, Performance Problems, Common date/time routines,
CROSS-TABS and PIVOT tables Part 1 & Part 2, Using APPLY Part 1 & Part 2, Splitting Delimited Strings
Post #1037414
Posted Tuesday, December 21, 2010 7:22 AM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Monday, July 21, 2014 8:35 AM
Points: 3,642, Visits: 72,429
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.


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
For tips on how to post your problems
Post #1037711
Posted Saturday, February 5, 2011 8:15 PM


SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Thursday, May 22, 2014 8:28 AM
Points: 89, Visits: 159
It gives an error......
Error: "Column name or number of supplied values does not match table definition


Thanks & Regards,
Kartik M Kumar..
Post #1059192
« Prev Topic | Next Topic »

Add to briefcase «««34567

Permissions Expand / Collapse