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 12345»»»

Things You Didn't Know About Temp Tables and Table Variables Expand / Collapse
Author
Message
Posted Wednesday, July 16, 2008 10:16 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Monday, September 1, 2014 1:54 AM
Points: 29, Visits: 153
Comments posted to this topic are about the item Things You Didn't Know About Temp Tables and Table Variables


Kindest Regards,

Roi Assa

Post #535656
Posted Thursday, July 17, 2008 1:03 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Yesterday @ 7:33 AM
Points: 2,542, Visits: 2,410
Another thing never said: when creating temp table is better adding the "collate" to char/varchar/text etc. cause of different collate in tempdb e the current db.
Post #535713
Posted Thursday, July 17, 2008 1:49 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Wednesday, September 17, 2014 1:05 AM
Points: 2,842, Visits: 3,875
Hello Roi,

you actually can add constraints to table variables.
Example:
DECLARE @MyTabVar TABLE(
PKCol int NOT NULL PRIMARY KEY CLUSTERED
,UniqueCol int NOT NULL UNIQUE
)



Best Regards,
Chris Büttner
Post #535734
Posted Thursday, July 17, 2008 4:10 AM


UDP Broadcaster

UDP BroadcasterUDP BroadcasterUDP BroadcasterUDP BroadcasterUDP BroadcasterUDP BroadcasterUDP BroadcasterUDP Broadcaster

Group: General Forum Members
Last Login: Wednesday, January 2, 2013 12:15 PM
Points: 1,443, Visits: 711
Nice work! Your article was very informative!
Post #535798
Posted Thursday, July 17, 2008 5:13 AM


Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Friday, September 12, 2008 8:25 AM
Points: 8, Visits: 43
remember you can use tempdb as a nearly normal database**

I create Structure for XML explict table[s] and error_log table[s] in tempdb and JOB-Agent bcp's these to files.xml

Their STRUCTURES/Programmes are copied into model
Every time SQL-2005 is restarted empty tables are created in tempdb,from model, and are then populated as required.

this data is available if error is found and correction to 'Standard' database table[s] can be made - otherwise I am not sad to loose-data on sql_shutdown! We have to many tables in standard named as xtemp****** but nobody remembers what they were for!

**An article on differences between Tempdb/Model and 'STANDARD' sql-tables is probably some-where, it is just that I have been doing it this way for ages!

Post #535845
Posted Thursday, July 17, 2008 6:04 AM


Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Monday, May 7, 2012 9:23 AM
Points: 304, Visits: 716
Great article, and thanks for it - but under the heading of "the more I learn the less I know"...

Is there really any difference or advantage between Temp Tables and Table variables? If there is not enough cache on hand and both will eventually hit TempDB - is the only advantage that Table variables dont hit it explicitly to start?

I ask this because we have an app that generates reports by calling stored procedures. So we are only grabbing data when a report is being prepped to run. But as we design stored procs and sometimes play with Temp Tables versus Unions, or Temp Tables versus Table variables, we have yet to see any major significant difference in performance and I am left wondering if these are all just pretty much the same?

Thanks again for a great post!


There's no such thing as dumb questions, only poorly thought-out answers...
Post #535883
Posted Thursday, July 17, 2008 6:23 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 3:11 PM
Points: 5,361, Visits: 8,924
Is there really any difference or advantage between Temp Tables and Table variables? If there is not enough cache on hand and both will eventually hit TempDB - is the only advantage that Table variables dont hit it explicitly to start?


One thing that you CAN do with a #temp table that you can NOT do with a @Table variable is to create indexes on columns. As another poster noted, you can create the PK (and thus the PK index) on a @Table variable - but only if it is included in the declare statement. Whereas with a #temp table, you can follow up the create table (or select into) statement with create index statements. This can give the #temp table a big performance benefit, especially if you are loading it with a lot of data.

I agree that this is an excellent article... especially the part about the named constraints.


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 #535896
Posted Thursday, July 17, 2008 6:38 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Wednesday, May 21, 2014 8:17 AM
Points: 297, Visits: 335
I'll be the one to ask the idiot question:

When is it advantageous to use a table variable? Since I started in Version 7, when they didn't exist, I just have never bothered to use one yet. When I need a temp table, I've never felt constrained by it - you can call another SP and let it reference your temp table which is what I thought was the big deal with table variables. You can't pass a table variable into a SP from ADO (now THAT would be cool).



Student of SQL and Golf, Master of Neither
Post #535906
Posted Thursday, July 17, 2008 6:46 AM


UDP Broadcaster

UDP BroadcasterUDP BroadcasterUDP BroadcasterUDP BroadcasterUDP BroadcasterUDP BroadcasterUDP BroadcasterUDP Broadcaster

Group: General Forum Members
Last Login: Wednesday, January 2, 2013 12:15 PM
Points: 1,443, Visits: 711
sbateman (7/17/2008)
remember you can use tempdb as a nearly normal database**

I create Structure for XML explict table[s] and error_log table[s] in tempdb and JOB-Agent bcp's these to files.xml

Their STRUCTURES/Programmes are copied into model
Every time SQL-2005 is restarted empty tables are created in tempdb,from model, and are then populated as required.

this data is available if error is found and correction to 'Standard' database table[s] can be made - otherwise I am not sad to loose-data on sql_shutdown! We have to many tables in standard named as xtemp****** but nobody remembers what they were for!

**An article on differences between Tempdb/Model and 'STANDARD' sql-tables is probably some-where, it is just that I have been doing it this way for ages!



Hmmm that makes sense... Grab the data out of tempdb...
Post #535915
Posted Thursday, July 17, 2008 7:28 AM


Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Friday, September 12, 2008 8:25 AM
Points: 8, Visits: 43
on area where I have been back to #table is after CTE

eg
;WITH cte_OldTraders (TRID, LastSOdate)
AS (
SELECT traderid, MAX(orderdate)
FROM salesorders
WHERE status != N'CANCELLED'
GROUP BY traderid
)
--
SELECT customers.id AS xtrid
, isNull(LastSOdate,customers.createddate) AS xsdat
INTO #soldtraders
FROM customers
LEFT OUTER JOIN cte_OldTraders ON (customers.id = TRID)
WHERE [tradingstatus] = N'CURRENT';
--
BEGIN TRAN
UPDATE traders
SET [tradingstatus] = N'DORMANT', [isonhold] = 0
FROM traders
INNER JOIN #soldtraders ON (id = xtrid)
WHERE [dbo].[xfn_julian_date](xsdat) < 3400 -- aging Function
COMMIT TRAN

I could not get @soldtraders working

so it is GOOD to have knowledge of both
Post #535957
« Prev Topic | Next Topic »

Add to briefcase 12345»»»

Permissions Expand / Collapse