tags:

views:

65

answers:

4

Please tell me how to make temporary tables in SQL. I am new to this area.

A: 

Assuming T-SQL:

DECLARE @ProductTotals TABLE
(
  ProductID int,
  Revenue money
)

INSERT INTO @ProductTotals (ProductID, Revenue)
  SELECT ProductID, SUM(UnitPrice * Quantity)
    FROM [Order Details]
    GROUP BY ProductID

UPDATE @ProductTotals
  SET Revenue = Revenue * 1.15
WHERE ProductID = 62

DELETE FROM @ProductTotals
WHERE ProductID = 60


SELECT TOP 5 *
FROM @ProductTotals
ORDER BY Revenue DESC

That is a table variable. There is also an in-memory table - instead of DECLARE @ProductTotals use CREATE TABLE #ProductTotals.

Use Table Variable for holding data during lifetime of SPROC/Function.

Use In Memory Tables for holding data over multiple SPROC's/child SPROC's. Needs to be explicitly dropped.

RPM1984
The #ProductTotals tables are **NOT** in-memory - they are persisted to disk. Also note: while #ProductTotals tables will participate in transactions, @ProductTotals (in-memory table variables) do not which might cause some surprises.
marc_s
@marc_s yeah i can never think of the name of the # tables. what is it? is it "temporary tables". if so, what is are the @ one's called. good point about transactions too.
RPM1984
@ProductTotals = in-memory table variable; #ProductTotals = temporary table (on disk, for this connection only, gone after connection is closed); ##ProductTotal = global temporary table, visible to all connections, persistent
marc_s
@marc_s. It's not true to assume table variables are any more in memory than temp tables are. Both create a table structure in tempdb and both will be held in memory if small enough. http://scarydba.wordpress.com/2009/10/13/table-variables-are-only-in-memory-fact-or-myth/
Martin Smith
@Martin Smith: true - table variables are potentially in memory - if small enough, right? #temp and ##temp tables never are as far as I know
marc_s
@marc - See page 97 here http://www.scribd.com/doc/24594811/Inside-Microsoft%C2%AE-SQL-Server%E2%84%A2-2005-T-SQL-Programming
Martin Smith
A: 

The two types of temporary tables, local and global, differ from each other in their names, their visibility, and their availability.

Local temporary tables have a single number sign (#) as the first character of their names; they are visible only to the current connection for the user; and they are deleted when the user disconnects from instances of Microsoft® SQL Server™ 2000.

CREATE TABLE #people 
( 
    id INT, 
    name VARCHAR(32) 
)

Global temporary tables have two number signs (##) as the first characters of their names;

CREATE TABLE ##people 
( 
    id INT, 
    name VARCHAR(32) 
)

Temporary Table variable Example (Sql Server)

DECLARE @TibetanYaks TABLE (
YakID int,
YakName char(30) )

INSERT INTO @TibetanYaks (YakID, YakName)
SELECT  YakID, YakName
FROM    dbo.Yaks
WHERE   YakType = 'Tibetan'

More aobut : Temporary Tables

Pranay Rana
Same google search i see. :)
griegs
And i see he edited his answer to provide more detail, after seeing mine had more. =)
RPM1984
@RPM1984, yeah but let's face it, Tibetan Yaks are far more interesting than product Totals. :)
griegs
true. who isn't interested in tibetan yaks? :)
RPM1984
I don't think I even what to know anyone that isn't
griegs
@RPM1984 and @griegs - you both are talking about the temporary tables only I added more details about global, local temp table and temporary variables -- and thanks for the info guys
Pranay Rana
And again: be aware that global temporary tables aren't deleted automatically - that's up to you. Also: while local and global temporary tables (on disk) do participate in transaction, table variables do not - which can lead to surprising results if you're not aware of that fact.
marc_s
+2  A: 
CREATE TABLE #Yaks (
YakID int,
YakName char(30) )

select name
from tempdb..sysobjects 
where name like '#yak%'

drop table #yaks

Did a Google search and found this as the first hit.

griegs
A: 
SELECT columnNames INTO #temp FROM TableName    
SELECT * FROM #temp    
DROP TABLE #temp
Muhammad Kashif Nadeem