Using Temporary Tables in SQL

This is how you can use SQL Temp Tables for doing some operations that might require a temporary table. A temporary table can be useful if you just want to have a data store of values you just require temporarily and not permanently sitting as a dedicated SQL table.

In this example, I am creating a temporary SQL table called tb1 with one column of type integer that will hold values from 0 to 10. This can be handy if you want to compare values either returned from a SQL Function or SQL Stored Procedures. You can also use temporary table to derive some values without actually changing the values in a SQL Table.

DROP TABLE #tempo1
CREATE TABLE #temp01(ID int)

DECLARE @i AS int
SET @i = 0

WHILE (@i < 10)
BEGIN
INSERT INTO #temp01 VALUES(@i)
SET @i += 1
END

SELECT * FROM #temp01
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s