SQL Server Tutorialprovides basic and advanced concepts of C# for beginners and professionals.

SQL Temporary Tables

Back to: SQL Server Tutorial

In MS SQL Server, temporary tables are used to store intermediate results temporarily. They are particularly useful when you need to break down complex queries into simpler steps or when you need to store data for the duration of a session or transaction. There are two types of temporary tables in SQL Server:

  1. Local Temporary Tables
  2. Global Temporary Tables

Local Temporary Tables

  • Syntax:

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

    • The table name starts with a # symbol (e.g., #TempTableName).
    • They are visible only within the session (or connection) that created them.
    • They are automatically dropped when the session ends or the connection is closed.
  • Example:

    CREATE TABLE #Employees ( EmployeeID INT, Name NVARCHAR(100) );
    INSERT INTO #Employees (EmployeeID, Name) VALUES (1, 'John Doe'), (2, 'Jane Smith');
    SELECT * FROM #Employees; -- No need to drop #Employees; it will be dropped automatically at the end of the session.

Global Temporary Tables

  • Syntax:

    CREATE TABLE ##GlobalTempTableName ( Column1 DataType, Column2 DataType, ... );
  • Characteristics:

    • The table name starts with ## (e.g., ##GlobalTempTableName).
    • They are visible to all sessions and connections.
    • They are dropped automatically when the last session referencing them is closed.
  • Example:

    CREATE TABLE ##GlobalEmployees ( EmployeeID INT, Name NVARCHAR(100) );
    INSERT INTO ##GlobalEmployees (EmployeeID, Name) VALUES (1, 'Alice Brown'), (2, 'Bob White'); -- This SELECT can be run from any session
    SELECT * FROM ##GlobalEmployees; -- The table will be dropped when the last session referencing it is closed.

Common Points

  1. Indexes and Constraints: Temporary tables can have indexes and constraints just like regular tables. This can be useful for improving performance or enforcing data integrity temporarily.

  2. Scope and Lifetime: The scope of local temporary tables is limited to the session, whereas global temporary tables are available to any session. Both types are automatically dropped by SQL Server when their scope ends, though you can also explicitly drop them using DROP TABLE.

  3. Performance Considerations: While temporary tables can be very useful, excessive use or improper indexing can lead to performance issues. Always consider whether a table variable or a common table expression (CTE) might be more appropriate for your specific needs.

  4. Table Variables: For smaller datasets or simpler use cases, table variables (declared using DECLARE @TableVariable TABLE (...)) might be more efficient than temporary tables.

Example of Dropping Temporary Tables

Although not necessary, you can explicitly drop a temporary table when you're done with it:

DROP TABLE #TempTableName; -- For local temp table
DROP TABLE ##GlobalTempTableName; -- For global temp table

By understanding and utilizing temporary tables effectively, you can manage and manipulate data more efficiently during your database operations.

Scroll to Top