|
|
|
SSC Rookie
      
Group: General Forum Members
Last Login: Sunday, April 28, 2013 1:08 AM
Points: 29,
Visits: 117
|
|
|
|
|
|
SSCommitted
      
Group: General Forum Members
Last Login: Yesterday @ 2:04 AM
Points: 1,968,
Visits: 1,819
|
|
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.
|
|
|
|
|
SSCrazy
      
Group: General Forum Members
Last Login: 2 days ago @ 3:36 AM
Points: 2,522,
Visits: 3,616
|
|
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
|
|
|
|
|
UDP Broadcaster
      
Group: General Forum Members
Last Login: Wednesday, January 02, 2013 12:15 PM
Points: 1,443,
Visits: 711
|
|
Nice work! Your article was very informative!
|
|
|
|
|
Forum 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!
|
|
|
|
|
Old Hand
      
Group: General Forum Members
Last Login: Monday, May 07, 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...
|
|
|
|
|
SSCertifiable
       
Group: General Forum Members
Last Login: Today @ 1:22 PM
Points: 6,367,
Visits: 8,225
|
|
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
|
|
|
|
|
SSC Veteran
      
Group: General Forum Members
Last Login: Monday, October 10, 2011 2:00 PM
Points: 297,
Visits: 329
|
|
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
|
|
|
|
|
UDP Broadcaster
      
Group: General Forum Members
Last Login: Wednesday, January 02, 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...
|
|
|
|
|
Forum 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
|
|
|
|