Temporary tables in SQL are used to store intermediate results and handle temporary data within a session. These tables are created in a special database called TempDB
and are automatically deleted once the session or connection that created them is terminated. They are especially useful for managing intermediate query results or for use in complex queries.
Creating and Using Temporary Tables
Syntax for Creating a Temporary Table:
CREATE TABLE #TempTableName (
column1 datatype,
column2 datatype,
...
);
Syntax for Inserting Data into a Temporary Table:
INSERT INTO #TempTableName (column1, column2, ...)
VALUES (value1, value2, ...), (value1, value2, ...);
Syntax for Selecting Data from a Temporary Table:
SELECT * FROM #TempTableName;
Types of Temporary Tables
There are two main types of temporary tables: Local Temporary Tables and Global Temporary Tables.
Local Temporary Table
A Local Temporary Table is visible only to the session or connection that created it. It is automatically dropped when the session ends. Local Temporary Tables are prefixed with a single #
symbol.
Example:
Let’s create a stored procedure that uses a local temporary table to store and retrieve some employee data.
CREATE PROCEDURE ProcLocalTemp
AS
BEGIN
-- Create a local temporary table
CREATE TABLE #EmployeeData (
employee_id INT,
employee_name VARCHAR(50)
);
-- Insert data into the local temporary table
INSERT INTO #EmployeeData (employee_id, employee_name)
VALUES (1, 'John Doe'), (2, 'Jane Smith');
-- Select data from the local temporary table
SELECT * FROM #EmployeeData;
END;
Execution:
EXEC ProcLocalTemp;
Result:
employee_id | employee_name |
---|---|
1 | John Doe |
2 | Jane Smith |
In this example, the local temporary table #EmployeeData
is created, used, and then dropped automatically when the stored procedure execution completes.
Global Temporary Table
A Global Temporary Table is visible to all sessions and connections. It is dropped only when the last connection referencing the table is closed. Global Temporary Tables are prefixed with ##
.
Example:
Create a global temporary table to store and retrieve data that can be accessed by multiple sessions.
CREATE TABLE ##GlobalEmployeeData (
employee_id INT,
employee_name VARCHAR(50)
);
-- Insert data into the global temporary table
INSERT INTO ##GlobalEmployeeData (employee_id, employee_name)
VALUES (1, 'Alice Cooper'), (2, 'Bob Brown');
-- Select data from the global temporary table
SELECT * FROM ##GlobalEmployeeData;
Result:
employee_id | employee_name |
---|---|
1 | Alice Cooper |
2 | Bob Brown |
In this case, the global temporary table ##GlobalEmployeeData
can be accessed by any session until it is dropped or all references to it are closed.
Key Points to Remember
- Local Temporary Tables (
#TableName
) are session-specific and automatically deleted when the session ends. - Global Temporary Tables (
##TableName
) are accessible across sessions and are dropped when the last connection referencing them is closed. - Temporary tables help in managing intermediate data and complex query processing efficiently.
Conclusion
Temporary tables are a powerful feature in SQL that allow for efficient handling of temporary data. Understanding the differences between local and global temporary tables, and knowing how to use them appropriately, can significantly enhance your ability to manage and manipulate data in complex queries.
For further learning, explore additional resources and practice using temporary tables in various SQL scenarios to master their use.