Introduction
Database managers and developers use temporary tables, also referred to as temp tables, frequently. They are stored in the tempDB database and work just like regular tables, allowing you to pick, add, and remove data as needed. If they are created during a stored procedure, they will be eliminated after the process is complete.
What is a Temporary Table in SQL Server?
A database table that is only momentarily present on the database server is referred to as a temporary table in SQL. A temporary table keeps a portion of a regular table's data for a predetermined period of time.
When you need to consistently interact with only a tiny portion of the numerous records in a table, temporary tables come in handy. In some cases, you can filter the data once and save it in a temporary table instead of filtering the data multiple times to get the subset.
Permanent tables and temporary tables are nearly identical. When the final connection to the query window that generated the table is closed, they are generated in TempDB and immediately removed. Interim results can be processed and stored using Temporary Tables. When we need to store temporary data, we use temporary tables.
How to Create a Temporary SQL Table?
CREATE TABLE #tmpEmp
(
Id INT,
Name NVARCHAR(50),
Rank INT
)
Types of Temporary Tables in SQL
There are a couple of temporary tables in SQL.
- Local Temporary Tables
- Global Temporary Tables
Local Temporary Tables
TempDB contains local temporary table storage. They are destroyed automatically at the conclusion of the operation or session, and they are only available to the one who created them.
For instance, after a local temporary table called #tmpEmp is created, the user's ability to manipulate the table is limited until the query window's final connection is closed. They can be recognized by the prefix #, such as #table name, and the same temporary table can be created with the same name in many windows.
A local temporary table can be created with the CREATE TABLE command, where the table name is prefixed with a single number sign (#table name).
Syntax
The following is the syntax in SQL Server (Transact-SQL) for making a LOCAL TEMPORARY TABLE.
CREATE TABLE #tablename
(
column1 datatype [ NULL | NOT NULL ],
column2 datatype [ NULL | NOT NULL ],
column3 datatype [ NULL | NOT NULL ],
...
columnn datatype [ NULL | NOT NULL ]
);

Let’s insert some data entry in a temporary table.
INSERT INTO #tmpEmp ([Name], [Rank])
VALUES ('Jaimin Shethiya', 196)

Let’s check the results, whether it’s stored or not.
SELECT Id, [Name], [Rank] FROM #tmpEmp

Where do I store the temporary table on the SQL Server?

Another way to create a temporary table in SQL is with the SELECT INTO statement. For the illustration, go through the following things.
SELECT Id, [Name], [Rank]
INTO #tmpEmp1
FROM #tmpEmp



Global Temporary Table in SQL Server
Additionally, they are kept in tempDB. These tables belong to the category of transient tables that are concurrently accessible to all users and sessions. When the final session using the temporary table concludes, they are automatically removed. These tables do not exist in the system catalogs and are not persistent.
A global temporary table is created with the CREATE TABLE command, and the table name is preceded by a double number sign (##table name).
Syntax
The following is the syntax in SQL Server (Transact-SQL) for making a global temporary table.
CREATE TABLE ##tablename
(
column1 datatype [ NULL | NOT NULL ],
column2 datatype [ NULL | NOT NULL ],
column3 datatype [ NULL | NOT NULL ],
...
columnn datatype [ NULL | NOT NULL ]
);
Create a global temporary table.







Join the conversation! Your thoughts help the community grow.