How To Create A Temp Table In SQL Server

Temp tables in SQL Server can be local or global. The name of the local temporary table starts with a # symbol, whereas the name of the Global temp table starts with a ## symbol. Below are two simple approaches to creating the temp table.

Approach 1: Using the standard Create statement

You can use the below query to create a local temp table. After executing this query, the temp table was created successfully, as shown in the screenshot below.

CREATE TABLE #Customer (
    CustomerID INT,
    CustomerName VARCHAR(50),
    Department VARCHAR(50)
);
how to create temp table in sql server

Let us run the query below to check if the temple has been created successfully.

Select * from #Customer;
create temp table sql server

Check out: How to insert into temp table in SQL Server

Execute the query below to create a global temp table. The screenshot below shows that the temp table was created successfully after executing this query.

CREATE TABLE ##Employee (
    EmployeeID INT,
    EmpName VARCHAR(50),
    Department VARCHAR(50)
);
how to create temp table sql server
sql server how to create temp table

Check out: How to drop temp table if it exists in SQL Server

Approach 2: Using the SELECT INTO statement.

You can also execute the below query to create a temp table.

SELECT Emp_id, Emp_name
INTO #TempEmployees
FROM Employee_new
WHERE Emp_id = 2;
create temp table sql server insert into

Conclusion

Creating temporary tables in SQL Server is a powerful technique that can significantly improve query performance and simplify complex data operations. Temporary tables play a crucial role in SQL Server and offer developers a flexible tool for managing intermediate results and optimizing queries. As mentioned in this article, you can create a temp table using the Create table SQL query or the SELECT INTO statement!

You may also like following the articles below