Temporary Tables In SQL Server

Why do we need temporary tables in SQL Server?

Often as a developer, we come upon a situation where we want to store some intermediate results temporarily. So in such cases, SQL Server provides us with a feature called temporary tables which can be created at runtime and used to store the data temporarily.

  • They can be used to create a workspace for processing the data inside stored procedure or functions.
  • They can be used for complex joins.
  • They get created either in ‘tempdb’ system database available SQL Server or in memory based on the choice made by query engine based on the size of the data getting stored.
  • All the operations which can be performed on a normal table can be performed on a temporary table as well.

Types of Temporary Tables

Based on the scope of the temporary table, we have two types of temporary tables in SQL Server.

  • Local temporary tables
  • Global temporary tables

Local Temporary table

  • We can create a local temporary table by using # before the table name.
  • They are available only for the current user session.
  • They get discarded automatically once the session has ended.
    1. /*Script for creating local temporary table*/  
    2. CREATE TABLE# temptable(ID intName varchar(50), )  
    3. GO  
    4. insert into# temptable values(1, 'Padmalatha');  
    5. GO  
    6. Select * from# temptable  

Once this query is executed, we can see that the temporary table named ‘temptable’ is created in ‘tempdb’ database inside ‘Temporary Tables’ folder and results are displayed as shown below.

Local temporary table created

SQL Server

Results

SQL Server

Global Temporary Table

  • We can create the global temporary tables using ## before the table name.
  • We can store the data in a global temporary table when we want to access the same data from different sessions. They are available for all users.
  • It gets discarded automatically when there is no active reference for that table.
    1. /*Script for creating Global temporary table*/  
    2. CREATE TABLE## globaltemp(TotalCount int, )  
    3. GO  
    4. insert into## globaltemp values(100);  
    5. GO  
    6. Select * from## globaltemp  

Once this query is executed, we can see that the global temporary table named ‘globaltemp’ is created in ‘tempdb’ database inside ‘Temporary Tables’ folder and results are displayed as shown below.

global temporary table created

SQL Server

Results

SQL Server

Table Variable

  • They are alternative options to temporary tables.
  • Their scope is limited only to batch or stored procedure or function in which they are declared.
  • ­They are declared using @ before a variable and table datatype.
  • Transaction logs are not created for table variables.

When can we go for Table Variable?

We can use table variable if the quantity of the data being stored is less. Indexes cannot be created on the table variable. So if we have a large quantity of data to be stored, then it is better to use temporary tables.

  1. /*Script for creating table variable*/  
  2. Declare @SampleTableVar table(Id int primary key, Total_Count int)  
  3. Insert @SampleTableVar values(1, 20)  
  4. Insert @SampleTableVar values(2, 30)  
  5. Insert @SampleTableVar values(3, 40)  
  6. Select * from @SampleTableVar  
  7. Go  

Results

SQL Server