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.
SELECT * FROM #TEMP_SAVE

Drop the local temp table.
DROP TABLE #TEMP_SAVE
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








Darshan ShahPosted Aug 28, 2020, 2:19 AM
Great Information
Amit MohantyPosted Jul 25, 2019, 12:04 AM
Nice article