Open In App

SQL | Declare Local Temporary Table

Last Updated : 16 Dec, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Local temporary tables in SQL are powerful for managing intermediate data during a session or procedure. These tables enable developers to temporarily store results, perform computations, and simplify complex queries. Understanding how to declare, use, and manage local temporary tables effectively can significantly enhance database performance and organization.

In this article, we will explain declaring local temporary tables in SQL, exploring their syntax, examples, and best practices. Along the way, we’ll explain key concepts with detailed examples and outputs, ensuring a complete understanding of this essential SQL feature.

Declaring Local Temporary Tables in SQL

Local temporary tables are a type of table in SQL that exists only during the session or scope in which they are created. These tables are stored in the tempdb database and are automatically dropped when the session or procedure ends. They are useful for temporarily storing intermediate results or data for specific tasks. They are automatically dropped when the session or procedure that created them ends.

Key Features of Local Temporary Tables:

  • They are session-specific, meaning they are accessible only within the session or scope that created them.
  • They are automatically deleted once the session ends, reducing manual cleanup.
  • Local temporary tables are denoted by a # symbol at the beginning of their name.

Syntax

CREATE TABLE #TempTableName
(
Column1 DataType,
Column2 DataType,
...
);

Key Terms

  • #TempTableName: The name of the temporary table, which must start with a # symbol to denote that it is a local temporary table.
  • Column1, Column2, ...: The columns of the table along with their data types.

Example 1: Declaring and Using a Local Temporary Table

Let’s create a local temporary table named #TempGeek, insert values into it, and retrieve the data. This example demonstrates how temporary tables can be utilized to store intermediate results. By using temporary tables, we can optimize queries for better performance and clarity.

Query:

DECLARE LOCAL TEMPORARY TABLE TempGeek ( number INT );

INSERT INTO Geeks
VALUES (1), (2), (3), (4);

Select *
from TempGeek;

Output

number
1
2
3
4

Explanation:

  • The CREATE TABLE statement defines the structure of the local temporary table #TempGeek with a single column named number of type INT.
  • Once we create a local temporary table, we cannot create another temporary table of that name for as long as the temporary table exists.

Example 2: Attempting to Create a Temporary Table with the Same Name

If we try to create a temporary table with the same name in the same session, SQL will throw an error. This ensures that each temporary table name within a session is unique, preventing conflicts and unintended data overwrites

Query:

declare local temporary table Geektable

Explanation:

  • Once a local temporary table is created, attempting to create another table with the same name in the same session results in an error.
  • This ensures unique naming and prevents conflicts within a session.

Example 3: Local Temporary Table Shadowing Base Tables

We can create a local temporary table with the same name as an existing base table. The local temporary table takes precedence within the session, effectively shadowing the base table until the temporary table is dropped or the session ends.

Query:

CREATE TABLE Geeks (num INT);
INSERT INTO Geeks VALUES (9), (8);

CREATE TABLE #Geeks (num INT);
INSERT INTO #Geeks VALUES (6), (7);

SELECT *
FROM #Geeks;

Output

num
6
7

Explanation:

  • The base table Geeks is created and populated with values (9, 8).
  • A local temporary table #Geeks with the same name is then created and populated with values (6, 7).
  • Any reference to Geeks within the session will prioritize the local temporary table #Geeks.

Example 4: Dropping a Local Temporary Table

Local temporary tables are automatically dropped when the session ends. However, if we no longer need the temporary table during the session, we can manually drop it using the DROP TABLE statement.

Query:

DROP TABLE #TempGeek;  

Explanation:

  • The DROP TABLE statement deletes the temporary table #TempGeek from the session.
  • This is useful for manually managing temporary tables when they are no longer needed.

Common Errors with Local Temporary Tables

  1. Creating Temporary Tables with Duplicate Names in the Same Session:
    • Attempting to create a temporary table with the same name in the same session throws an error.
    • Ensure unique names or drop the existing table before creating a new one.
  2. Referencing Dropped Temporary Tables:
    • Once a temporary table is dropped, it cannot be referenced.
    • Always verify the existence of a temporary table before using it in queries.

Conclusion

Local temporary tables are an indispensable feature of SQL for managing temporary data within the scope of a session or procedure. They provide a flexible, efficient, and organized way to handle intermediate results, perform complex computations, and simplify query logic. By understanding how to declare, use, and manage local temporary tables effectively, we can enhance the performance and clarity of our SQL operations.


Next Article
Article Tags :

Similar Reads