Temporary Table in SQL

Last Updated : 21 Aug, 2026

A temporary table in SQL is a special table used to store data temporarily during query execution. It helps hold intermediate results without affecting permanent tables.

  • Stored in the system’s temporary database (like TempDB in SQL Server).
  • Automatically deleted when the session ends.
  • Useful for calculations or data processing without changing permanent data.

Syntax

  • To Create a Temporary Table
CREATE TABLE #empdetails (id INT, name VARCHAR(25)) 
  • To Insert Values Into Temporary Table
INSERT INTO #empdetails VALUES (01, 'James'), (02, 'Mike') 
  • To Select Values from the Temporary Table
SELECT * FROM #empdetails

Output:

Screenshot-2026-02-06-163548

Types of Temporary Tables in SQL

1. Local Temporary Table

A Local Temporary Table is a temporary table used to store intermediate data during query execution or processing. It is created using a single # prefix (e.g., #TempTable).

Query:

CREATE PROCEDURE ProcTemp
AS
BEGIN
CREATE TABLE #empdetails(
emp_id INT,
emp_name VARCHAR(50)
);
INSERT INTO #empdetails (EmpID, EmpName)
VALUES (1, 'Emilly'), (2, 'Steve');

SELECT * FROM #empdetails;
END;
GO

EXEC proc_temp ;

Output:

62

2. Global Temporary Table

A Global Temporary Table is a temporary table used to store temporary data that can be shared across multiple operations. It is created using a double ## prefix (e.g., ##TempTable).

Query:

CREATE TABLE ##empdetails (
emp_id INT,
emp_name VARCHAR(50)
);

Local vs. Global Temporary Tables

Local Temporary TableGlobal Temporary Table
Accessible only to the session that created it.

Accessible to multiple sessions.

Automatically dropped when the session ends.Dropped when the creating session ends and no other session is referencing it.
Used for session-specific data storage.Used for shared temporary data storage across multiple sessions.
Use when temporary data is needed only by one user or session.Use when temporary data needs to be shared among multiple users or sessions.
Comment