Local vs Global Temporary Table

1. Local temporary table

Syntax:

-- local temporary
select EnglishProductName, ListPrice - DealerPrice as Discount 
into #NewDimProduct
from DimProduct
where ListPrice - DealerPrice is not null
order by EnglishProductName
-- test
select * from #NewDimProduct
-- drop
drop table #NewDimProduct
select * from #NewDimProduct

Analysis:

The # prefix to the new table nameresults in the creation of a new temporary table. A single # prefix means a local temporary table. Before you run the DROP TABLE statement, it’s worth considering a few things. As the table is temporary, it will automatically disappear if you close your query editor window. Also, as it’s a temporary table, it lives in your tempdb database. You can view it in Object Explorer --- open the System Database folder, then tempdb, and expand Temporary Tables (you may need to right-click and choose Refresh). It’s a local temporary table; it can only be seen from the current query editor window. If you were to open a new window, a normal Select on the table would fail.

   Temporary tables are often used as work tables or staging tables or test tables. Maybe you want to experiment with some SQL, but prefer not to do so on a production table.

2. Global Temporary Table

Syntax:

-- global temporary
select EnglishProductName, ListPrice - DealerPrice as Discount 
into ##NewDimProduct
from DimProduct
where ListPrice - DealerPrice is not null
order by EnglishProductName
-- test
select * from ##NewDimProduct
-- drop
drop table ##NewDimProduct
select * from ##NewDimProduct

Analysis:

A double ## prefix means a global temporary table. It will reside in your tempdb database until you close the query editor window or issue a Drop Table. In those respects, it’s the same as a local temporary table. The difference lies in its scope. A local temporary table is only accessible from the current query editor window(called a session). On the other hand, a global temporary table is visible (during its lifetime) from other query editor windows or sessions. Global temporary tables can be used from testing or as staging tables when you want to start a new SQL query in a new window.

3. Semipermanent Temporary Table

Take a careful look at the first query here. It’s another Select Into but the table name is qualified with a schema and a database name. The database in question is the system database, tempdb.

Syntax:

-- semi-permanent temporary
select EnglishProductName, ListPrice - DealerPrice as Discount 
into tempdb.dbo.NewDimProduct
from DimProduct
where ListPrice - DealerPrice is not null
order by EnglishProductName
-- test
select * from tempdb.dbo.NewDimProduct
-- drop
drop table tempdb.dbo.NewDimProduct
select * from tempdb.dbo.NewDimProduct

Analysis:

You can explicitly create the new table in tempdb. But you won’t see it in the Object Explorer underneath the tempdb Temporary Tables folder. Rather, it’s under the tempdb Tables folder. It’s a permanent table in tempdb(your temporary database). They are not truly temporary --- if you close the query editor windows, the tables are still there for other query editor windows and users. They act just like permanent tables in normal production databases.

But then, they are not truly permanent --- if you stop and restart SQL Server, they are erased from tempdb. The tempdb database is always re-created and cleared as a result when SQL Server starts up. Again, these tables can be used as test or staging tables.

 

4. Appendix:

Query the temporary tables exist?

if object_id('tempdb..#TC_TableRelation') is null
begin
    CREATE TABLE [#TC_TableRelation](
        [ID] [nvarchar](255) NOT NULL,
        [ViewName] [nvarchar](50) NOT NULL,
        [MasterTableName] [nvarchar](255) NULL,
        [PrimaryKey] [nvarchar](50) NOT NULL,
        [PrimaryValue] [nvarchar](255) NULL,
        [Status] [nvarchar](255) NULL
    ) ON [PRIMARY]
end

Or

if exists (select * from tempdb.dbo.sysobjects where id = object_id(N'tempdb..#TC_TableRelation') and type='U')
   drop table #TC_TableRelation
posted @ 2013-06-21 14:21  AOT  阅读(546)  评论(0编辑  收藏  举报