Temporary tables are tables available only to the session that created them.
These tables are automatically destroyed at the termination of the procedure or session that created them. SQL Server provides the concept of temporary table that helps the developer in a great way. These tables can be created at runtime and can do many kinds of operations that a normal table can do. In SQL Server all temporary tables are present in the tempdb database.
Types of Temporary Tables
SQL Server contains the following two types of Temporary tables.
- Local Temporary Tables
- Global Temporary Tables
Local Temporary Tables:
Local temporary tables are temporary 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. Local temp tables are only available to the current connection for the user. Names of Local temp tables starts with (“#”) hash sign.
Example
- CREATE TABLE #TEMP_SAVE
- (
- Id INT,
- Name VARCHAR(30),
- Date DATETIME DEFAULT GETDATE()
- )
Global Temporary Tables
Global temporary tables are temporary tables that are available to all sessions and all users. Once this table has been created by a connection, like a permanent table then it is available to any user by any connection. They are dropped automatically when the last session using the temporary table has completed. Names of global temp tables start with (“##”) double hash sign.
Example
- CREATE TABLE ##TEMP_SAVE
- (
- Id INT,
- Name VARCHAR(30),
- Date DATETIME DEFAULT GETDATE()
- )
Now we will examine some examples of temp tables.
Example 1
Create a local temp table.
- CREATE TABLE #TEMP_SAVE
- (
- Id INT,
- Name VARCHAR(30),
- Age int
- )
Now insert data into the local table.
- Insert into #TEMP_SAVE
- Select 1,'pankaj',20 Union All
- Select 2,'Rahul',21 Union All
- Select 3,'Sandeep',22 Union All
- Select 4,'Sanjeev',23 Union All
- Select 5,'Neeraj',24
Fetch data from the local temp table.
Drop the local temp table.
Example 2
Create a global temp table.
- Create Table ##TEmp
- (
- IID int identity(1,1),
- Name nvarchar(50),
- Salary int
- )
Insert data into the global temp table.
- Insert Into ##TEmp
- Select 'Pankaj', 25000 Union All
- Select 'Rahul', 24000 Union All
- Select 'Sanjeev', 23000 Union All
- Select 'Sandeep', 22000 Union All
- Select 'Naru', 27000
Fetch data the from global temp table.
Drop the temp table.
Example 3
We have an Employee table that look like the following:
Now we will create a local Temp using the “
Select Into” command.
- Select * Into #Temp_Loc
- From Employee
- Select * From #Temp_Loc
OutputExample 4
Now we will try to create a view for the temp table.
- Create View My_View
- as
- Select * from #Temp_Loc
- Where Emp_Id>3
Output
Msg 4508, Level 16, State 1, Procedure My_View, Line 4 Views or functions are not allowed on temporary tables. Table names that begin with '#' denote temporary tables.
So this example illustrates that we cannot create a view for a temp table.
Example 5
- Select * From ##TEmp
- Union All
- Select * From #TEMP_SAVE
OutputThis example illustrates that we can use Union and Union All for temp tables.
Example 6
- Select * From #Temp_Loc
- Left Outer Join
- ##TEmp
- ON
- #Temp_Loc.Emp_ID=##TEmp.IID
OutputThis example illustrates how to use Joins on temp tables. We can use all types of joins (INNER, LEFT OUTER, RIGHT OUTER , CROSS JOIN , SELF JOIN) on temp tables.
Example 7
Let us assume we have a table.
This table contains duplicate data. Now we will use the temp table to remove the duplicate data.
- /* Insert Data into temp Table */
-
- Select Distinct * into #Temp_tab
- from Employee_Detail
-
- /* Delete Data from Table */
- Delete from Employee_Detail
-
- /* Insert data from Temp table into Employee_Detail Table */
-
- insert into Employee_Detail
- select * from #Temp_tab
-
- /* Drop Temp Table */
- Drop Table #Temp_tab
-
- select * from Employee_Detail
Output
This example illustrate that we can use temp tables for holding the data that we will use further in a query. In the preceding example we used a temp table for removing the duplicate data.
When to use temporary tables?
- To hold data for further query.
- When we have a complex joins operation.
- In some cases it may be a replacement of cursors.
So in this article we saw how to create a temporary table, global temporary table and how to fetch the data.