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)
);
Let us run the query below to check if the temple has been created successfully.
Select * from #Customer;
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)
);
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;
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
Grey is a highly experienced and certified database expert with over 15 years of hands-on experience in designing, implementing, and managing complex database systems. Currently employed at WEX, USA, Grey has established a reputation as a go-to resource for all things related to database management, particularly in Microsoft SQL Server and Oracle environments. He is a Certified Microsoft SQL Server Professional (MCSE: Data Management and Analytics) and Oracle Certified Professional (OCP), with Extensive database performance tuning and optimization knowledge and a proven track record in designing and implementing high-availability database solutions.