Published on

December 2, 2014

Exploring Temporary Objects in SQL Server

SQL Server’s version of Transact SQL provides the ability to create and leverage temporary objects for use within the scope of your query session or batch. Temporary objects can be beneficial from a performance and scalability perspective, in addition to meeting various data-tier requirements.

Why use temporary objects?

There are several reasons for using temporary objects:

  • No requirement for persistent data storage for a result set.
  • Intermediate state needed only temporarily, for example when performing data transformation or aggregation.
  • Use of temporary storage only visible to the current session.
  • Reduce query plan complexity.
  • Portability of lookup-data by the containing modules, for example stored procedures.

Temporary objects can be created in SQL Server using temporary tables, table variables, or temporary storage procedures.

Creating a temporary table

To create a temporary table, you can use the following syntax:

CREATE TABLE #TempTable (
  ID INT,
  Name VARCHAR(50)
)

Once the temporary table is created, you can insert data into it and select data from it:

INSERT INTO #TempTable (ID, Name)
SELECT ID, Name
FROM PermanentTable

SELECT *
FROM #TempTable

To remove the temporary table explicitly, you can execute the following query:

DROP TABLE #TempTable

Using SELECT INTO with a temporary table

You can also create a temporary table implicitly using the SELECT INTO statement. The following example creates a temporary table and populates it with data from a permanent table:

SELECT *
INTO #TempTable
FROM PermanentTable

If you need an empty temporary table based on the schema of an existing table, you can use the following syntax:

SELECT *
INTO #TempTable
FROM PermanentTable
WHERE 1 = 0

Temporary table scope and lifecycle

A temporary table exists only within the scope of the current session or batch. If you disconnect and connect back to an SQL Server instance, the temporary table will no longer exist. It is important to explicitly drop the temporary table when it is no longer needed to free up system resources.

Viewing temporary table metadata

Once a temporary table is created, you can execute a system-stored procedure to view information about the table:

EXEC tempdb.sys.sp_help '#TempTable'

This will provide information about the columns, data types, and other properties of the temporary table.

Table variables

In addition to temporary tables, SQL Server provides table variables as an alternative. Table variables store a set of records and can be useful in certain scenarios. The syntax for declaring a table variable is similar to creating a temporary table:

DECLARE @TableVariable TABLE (
  ID INT,
  Name VARCHAR(50)
)

Table variables have a shorter lifecycle compared to temporary tables and do not need to be explicitly dropped. However, they cannot cross batch boundaries.

Advantages and disadvantages of using temporary objects

Temporary tables have the advantage of providing intermediate result sets, reducing query plan complexity, and allowing for column-level statistics. However, they can lead to heavy tempdb usage and are not supported in user-defined functions.

Table variables, on the other hand, can be used within scalar and multi-statement table-valued functions, inherit the current database’s collation implicitly, and can be passed as input parameters to stored procedures. However, they have limitations such as non-column level statistics and cannot perform certain operations like TRUNCATE or SELECT INTO.

When using temporary objects, it is important to consider the performance and scalability implications. Breaking down complex queries into steps with temporary objects can improve query plan quality and increase performance.

Click to rate this post!
[Total: 0 Average: 0]

Let's work together

Send us a message or book free introductory meeting with us using button below.