Temporary and Global Temporary Table in SQL Server 2008

Temporary Table in SQL Server

 
Temporary tables are tables that are available only to the session that created them.
 
These tables are automatically destroyed at the termination of the procedure or session that created them.
 
Use of temporary tables in MS SQL Server is more developer friendly and they are widely used in development. Local temporary tables are visible only in the current session.
 
Temporary tables are created using the same syntax as a CREATE TABLE except the table name starts with a '#' sign. When the table consists of a single '#' sign, it is defined as a local temporary table and its scope is limited to the session it is created in.
 

Global Temporary Table in SQL Server

 
Global Temporary tables are visible to or available across all sessions. And all users.
 
Global Temporary tables are created using the same syntax as a CREATE TABLE except the table name starts with "##" (two '#' signs). When the table is only "##", it is defined as a local global temporary table and it's scope is not limited to the session it is created in.
 
A Global Temporary table is dropped automatically when the last session using the temporary table has completed.
 
Both the local temporary tables and global temporary tables are physical.
 

Uses of Temporary Tables

 
A Temporary Table variable can be very useful when used with stored procedures to pass input/output parameters or to store the result of a table valued function.
 
Now to create the Temporary table the query will be:
  1. CREATE TABLE #TEMPTABLE  
  2. (  
  3. Id INT,  
  4. Name VARCHAR(30),  
  5. Date DATETIME DEFAULT GETDATE()  
  6. )  
Run the query. After that the "TEMPTABLE" will be created.
 
Now we are going to insert some values into the TempTable.
  1. INSERT INTO #TEMPTABLE(Id, NameVALUES(1,'shirsendu');  
  2. INSERT INTO #TEMPTABLE(Id, NameVALUES(2,'Sarnali');  
  3. INSERT INTO #TEMPTABLE(Id, NameVALUES(3,'Mrinal');  
Execute the Query
 
Now to see the values inserted into the temp table execute the following query:
  1. select * from  #TEMPTABLE  
The result will look like
 
Temporary Table in SQL Server 
 
Now to create the Global Temporary Table.
 
For creating the table we already know to use "##" before the table name.
  1. CREATE TABLE ##GLOBALTEMPTABLE  
  2. (  
  3. Id INT,  
  4. Address VARCHAR(30),  
  5. Date DATETIME DEFAULT GETDATE()  
  6. )  
Run the query. After that the "GLOBALTEMPTABLE" is created.
 
Now we are going to insert some values into the GLOBALTEMPTABLE.
  1. INSERT INTO ##GLOBALTEMPTABLE(Id, Address) VALUES(1,'Bangalore');  
  2. INSERT INTO ##GLOBALTEMPTABLE(Id, Address) VALUES(2,'Bangkok');  
  3. INSERT INTO ##GLOBALTEMPTABLE(Id, Address) VALUES(3,'CAlcutta');  
Now to see the values inserted into the GLOBALTEMPTABLE execute the following query.
  1. select * from  ##GLOBALTEMPTABLE  
The result will look like:
 
Global Temporary Table in SQL Server 
 
Now you can cross join between the Temporary table and the GlobalTemporaryTable as in:
  1. select * from ##GLOBALTEMPTABLE,#TEMPTABLE
The result will look like: 
 
SQLTable3.gif 

Conclusion

 
So in this article, we have seen how to create a temporary table and a global temporary table and how the data can be fetched. 


Similar Articles