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.

-- temp tables
CREATE TABLE #temp_People (
PeopleId int NOT NULL,
FirstName varchar(50) NULL,
LastName varchar(50) NULL,
Payment float NULL
);

INSERT INTO #temp_People
VALUES (1, 'Bob', 'Smith', 500.00);
INSERT INTO #temp_People
VALUES (2, 'Sue', 'Patel', 0.00);
INSERT INTO #temp_People
VALUES (3, 'Linda', 'Wong', 800.00);

SELECT * FROM #temp_People;

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

SELECT *
INTO #nh
FROM NashvilleHousing
WHERE SalePrice > 500000;

select * from #nh

Leave a Reply