SQL Temp Tables


Let’s discuss temporary tables in Microsoft’s T-SQL. Temporary tables have a hash (#) at the start of their name.

Here is a YouTube video by Alex The Analyst called Advanced SQL Tutorial | Temp Tables.

You can do many of the things with temp tables as you would do with regular tables.

1-- temp tables
2CREATE TABLE #temp_People (
3PeopleId int NOT NULL,
4FirstName varchar(50) NULL,
5LastName varchar(50) NULL,
6Payment float NULL
7);
8 
9INSERT INTO #temp_People
10VALUES (1, 'Bob', 'Smith', 500.00);
11INSERT INTO #temp_People
12VALUES (2, 'Sue', 'Patel', 0.00);
13INSERT INTO #temp_People
14VALUES (3, 'Linda', 'Wong', 800.00);
15 
16SELECT * FROM #temp_People;

The SELECT INTO statement copies data from one table into a new table.

1SELECT *
2INTO #nh
3FROM NashvilleHousing
4WHERE SalePrice > 500000;
5 
6select * from #nh

Leave a Reply