Select Temporary Table SQL Server

Hello Dev, if you are looking for a temporary table in SQL Server, then this article is for you. In this article, we will discuss how to select temporary tables in SQL Server. Temporary tables are used to store data temporarily during the execution of a query or a stored procedure. These tables are created inside the tempdb database and are automatically deleted when the session ends.

What is a Temporary Table?

A temporary table is a table that is created and used for storing data temporarily. These tables are created inside the tempdb database of SQL Server. The temporary table is only available for the current session and is automatically deleted when the session ends. Temporary tables are useful in a variety of situations, including complex queries that require several steps, or when you need to store data for a short amount of time.

Temporary tables can be created using the CREATE TABLE statement, and you can query or insert data into them just like any other table.

Creating a Temporary Table

The syntax of the CREATE TABLE statement for creating a temporary table is as follows:

Statement
Description
CREATE TABLE #temp_table (
column1 datatype,
column2 datatype,

)
This statement creates a temporary table with the specified columns and data types.

Here, the “#” symbol is used to indicate that this table is a temporary table.

For example, let’s create a temporary table with the name “temp_sales” and two columns, “SalesID” and “Amount”.

Statement
Description
CREATE TABLE #temp_sales (
SalesID int,
Amount money
)
This statement creates a temporary table with two columns, SalesID and Amount.

Inserting Data into a Temporary Table

Once a temporary table is created, you can insert data into it using the INSERT INTO statement. The syntax for the INSERT INTO statement is the same as for any other table.

For example, let’s insert some data into the temporary table we created in the previous example.

Statement
Description
INSERT INTO #temp_sales (SalesID, Amount)
VALUES (1, 100.00),
(2, 200.00),
(3, 300.00)
This statement inserts three rows of data into the temporary table.

Viewing Data in a Temporary Table

Once data is inserted into a temporary table, you can view it using the SELECT statement, just like any other table.

For example, let’s view the data in the temporary table we created earlier.

Statement
Description
SELECT * FROM #temp_sales
This statement selects all data from the temporary table.

This will return the following result:

SalesID
Amount
1
100.00
2
200.00
3
300.00

How to Select a Temporary Table

Now that we know how to create and insert data into a temporary table, let’s discuss how to select data from it.

To select data from a temporary table, you need to use the same SELECT statement that you use for regular tables. However, you need to include the “#” symbol before the table name to indicate that it is a temporary table.

For example, let’s select data from the temporary table we created earlier.

Statement
Description
SELECT * FROM #temp_sales
This statement selects all data from the temporary table.

This will return the following result:

SalesID
Amount
1
100.00
2
200.00
3
300.00

Using Where Clause with Temporary Tables

You can also use the WHERE clause to filter data from temporary tables, just like with regular tables.

For example, let’s select only the data where the SalesID is greater than 1.

READ ALSO  Understanding SQL Server Synonym: Everything Dev Needs to Know
Statement
Description
SELECT * FROM #temp_sales
WHERE SalesID > 1
This statement selects only the data where the SalesID is greater than 1.

This will return the following result:

SalesID
Amount
2
200.00
3
300.00

Using Order By Clause with Temporary Tables

You can also use the ORDER BY clause to sort data from temporary tables, just like with regular tables.

For example, let’s select the data from the temporary table we created earlier and order it by the Amount column in descending order.

Statement
Description
SELECT * FROM #temp_sales
ORDER BY Amount DESC
This statement selects all data from the temporary table and orders it by the Amount column in descending order.

This will return the following result:

SalesID
Amount
3
300.00
2
200.00
1
100.00

Using Group By Clause with Temporary Tables

You can also use the GROUP BY clause to group data from temporary tables, just like with regular tables.

For example, let’s group the data from the temporary table we created earlier by the Amount column.

Statement
Description
SELECT SUM(Amount) AS TotalAmount
FROM #temp_sales
GROUP BY Amount
This statement groups all data from the temporary table by the Amount column and calculates the sum of the Amount column for each group.

This will return the following result:

TotalAmount
100.00
200.00
300.00

FAQ

What is the difference between a temporary table and a table variable?

A temporary table is created and stored in the tempdb database and is automatically deleted when the session ends. A table variable is created in memory and is automatically deleted when the batch, stored procedure or function ends. The performance of table variables is generally better than temporary tables, but table variables have some limitations, such as not supporting indexes or statistics.

How can I delete a temporary table?

A temporary table is automatically deleted when the session ends, but you can also delete it explicitly using the DROP TABLE statement. The syntax for the DROP TABLE statement is the same as for regular tables.

For example, to delete the temporary table we created earlier, we can use the following statement:

Statement
Description
DROP TABLE #temp_sales
This statement deletes the temporary table we created earlier.

Can I create indexes on temporary tables?

Yes, you can create indexes on temporary tables using the CREATE INDEX statement, just like with regular tables. However, keep in mind that creating indexes on temporary tables can impact performance since they are recreated every time the table is created.

Can I use temporary tables in a clustered environment?

Yes, you can use temporary tables in a clustered environment, but keep in mind that each instance of SQL Server has its own tempdb database, so if you have multiple instances in the cluster, each instance will have its own set of temporary tables.