Temp Table in SQL Server

The temp table in SQL Server can be created at the run-time, and perform all the operation that a normal table can do. There are two types of Temporary Tables in SQL Server: Local Temporary Tables, and Global Temporary Tables. In this article we will show you, How to work with both Local, and Global Temp table in SQL.

Before we get into the examples, following list will show you, where we can use the SQL temp tables:

  When we are working with the complex joins.

  Temp tables are useful to replace the costly cursors. We can use this temp table to store the result set data, and then we can manipulate the data from the temp table.

  We can use this, when we are doing large number of row manipulation in stored procedures. Remember, If we create a temp table inside the stored procedure, then it will be applicable to that SP only. It means, you can not call the temp table from outside the stored procedure.

Local Temp Table in SQL Server

The name of the Local temporary table starts with the hash (#) symbol, and it is stored in the tempdb. Local temporary tables are available only in the current connection. If the user disconnects from current instances, then they are automatically deleted. Or, If we close the Query Window then the table will be dropped.

Local Temp Table in SQL Server Syntax

The following code snippet will show you the syntax behind the local temporary tables in SQL

CREATE TABLE #[Local Temp Table]
(
    Column_Name1 Data_Type(Size) [NULL | NOT NULL],
    Column_Name2 Data_Type(Size) [NULL | NOT NULL],
    Column_NameN Data_Type(Size) [NULL | NOT NULL]
);

 

Here, Table Name of a local temporary table should start with #. Remember, Please provide Unique table name here. If you write the already existing table name, it will throw an error

Create Local Temp Table in SQL Server Example

We are going to use the below shown code to create local temporary table called LocalTemp.

SQL CODE

-- Creating Local Temp Table in SQL Server
CREATE TABLE #LocalTemp
(
[ID] [int] IDENTITY(1,1) NOT NULL,
[FirstName] [nvarchar](255) NULL,
[LastName] [nvarchar](255) NULL,
[Occupation] [nvarchar](255) NULL,
[YearlyIncome] [float] NULL,
[Sales] [float] NULL
)

 

From the above code you can observe that, We declared 6 Columns. Here, Our first column is ID of Integer data type, and it will not allow NULL values. We also defined this column as Identity starting with 1 and incremented by 1.

OUTPUT

Create Temp Table in SQL Server 1

From the above screenshot you can observe that, Command is executed successfully. Please refresh the object explorer to see the Newly created Temporary Local table

Create Temp Table in SQL Server 2

Insert Data into Local Temp Table in SQL Server

Let me insert few random, or sample records into the local temporary table that we created inside the tempdb using the INSERT Statement.

SQL CODE

-- Inserting Values into Local SQL Temp Table
INSERT INTO #LocalTemp (
    [FirstName], [LastName], [Occupation], [YearlyIncome], [Sales])
VALUES ('Bob', 'Ward', 'Finance', 20000, 300)
      ,('Anil', 'Jain', 'Software Professional', 30000, 200)
      ,('Scott', 'Ford', 'Trainer', 25000, 40)
      ,('Amit', 'Kumar', 'Hr Executive', 35000, 890)

 

OUTPUT

Create Temp Table in SQL Server 3

Select Data from Local Temp Table in SQL Server

From the above screenshot you can see that, we successfully inserted 4 random records into the #LocalTemp table. Let me use the SELECT Statement to select the records present in the temp table.

SQL CODE

-- Selecting Values From Local SQL Temp Table

SELECT [FirstName],
       [LastName],
       [Occupation],
       [YearlyIncome],
       [Sales]
FROM #LocalTemp

 

OUTPUT

Create Temp Table in SQL Server 4

Until now, you might be wonder, why I am writing the Create, Insert, and Select statements in one query window?. This is because, local temporary tables will last up to single session, and if you try to call this table from new query window then it will thrown an error. For example, let me call the #LocalTemp table from new query window

Create Temp Table in SQL Server 5

As you can see, it is throwing an error stating that, Invalid Object name #LocalTemp. Now, let me close all the existing query windows, and refresh the tempdb from Object Explorer

Create Temp Table in SQL Server 6

From the above screenshot you can see that, it does not contains our #LocalTemp table.

Global Temp Table in SQL Server

The name of the Global temporary table starts with the double hash (##) symbol, and it is stored in the tempdb. Global temporary tables are like permanent tables, and they are available to all the users in that instance. If all the user disconnects from their session, then global temp tables will automatically deleted.

Global Temp Table in SQL Server Syntax

The following code will show you the syntax behind the Global temporary tables in SQL

CREATE TABLE ##[Global Temp Table Name]
(
    Column_Name1 Data_Type(Size) [NULL | NOT NULL],
    Column_Name2 Data_Type(Size) [NULL | NOT NULL],
    
    Column_NameN Data_Type(Size) [NULL | NOT NULL]
);

 

As I mentioned above, Table Name of a global temporary table should start with ##.Remember, Please provide Unique table name here. If you write the already existing table name, it will throw an error

Create Global Temp Table in SQL Server Example

We are going to use the below shown code to create global temporary table called GlobalTemp.

SQL CODE

--
-- Creating Global Temp Table in SQL Server
CREATE TABLE ##GlobalTemp
(
[ID] [int] IDENTITY(1,1) NOT NULL,
[FirstName] [nvarchar](255) NULL,
[LastName] [nvarchar](255) NULL,
[Education] [nvarchar](255) NULL,
[Occupation] [nvarchar](255) NULL,
[YearlyIncome] [float] NULL,
[Sales] [float] NULL
)

 

From the above code you can observe that, We declared 7 Columns.

OUTPUT

Temp Table in SQL Server 7

From the above screenshot you can observe that, Command is executed successfully. Please refresh the object explorer to see the Newly created Temporary Global table

Temp Table in SQL Server 8

Insert Data into Global Temp Table in SQL Server

Let me insert few sample, or random records into the global temp table that we created inside the tempdb using the INSERT Statement.

SQL CODE

-- Inserting Values into Global SQL Temp Table
INSERT INTO ##GlobalTemp (FirstName], [LastName], [Education], [Occupation], [YearlyIncome], [Sales]

VALUES('Anil', 'Kumar', 'Masters Degree', 'MANAGER', 65000, 300), ('Denial', 'Scott', 'Bachelors', 'ANALYST', 50000, 200), ('Smith', 'Miller', 'Degree', 'SALESMAN', 45000, 120), ('Sohani', 'Bhatt', 'Bachelors', 'ANALYST', 78000, 870)

 

OUTPUT

Temp Table in SQL Server 9

Select from Global Temp Table in SQL Server

From the above screenshot you can see that, we successfully inserted 4 random records into the ##GlobalTemp table. Let me use the SELECT Statement to select the records present in that temp table.

SQL CODE

-- Selecting Values From Global SQL Temp Table
SELECT [ID],
       [FirstName],
       [LastName],
       [Education],
       [Occupation],
       [YearlyIncome],
       [Sales]
FROM ##GlobalTemp

 

OUTPUT

Temp Table in SQL Server 10

Next, let me call the ##GlobalTemp table from new query window

Temp Table in SQL Server 11

As you can see, it is displaying the records, rather than throwing an error. Now, let me close all the existing query windows, and refresh the tempdb from Object Explorer

Create Temp Table in SQL Server 6

Now you can see that, there are no temp tables in our tempdb database.