Click here to Skip to main content
11,648,785 members (78,525 online)
Click here to Skip to main content

Quick Overview: Temporary Tables in SQL Server 2005

, 22 Sep 2009 CPOL 1M 94
Rate this:
Please Sign up or sign in to vote.
Basic overview of temporary Tables in SQL Server 2005

Table of Contents

Introduction

SQL Server provides the concept of temporary table which helps the developer in a great way. These tables can be created at runtime and can do the all kinds of operations that one normal table can do. But, based on the table types, the scope is limited. These tables are created inside tempdb database.

In this article, I am just going to give a quick overview for beginners on those temporary tables. Please give your valuable suggestions and feedback to improve this article.  

Different Types of Temporary Tables

SQL Server provides two types of temp tables based on the behavior and scope of the table. These are:

  • Local Temp Table
  • Global Temp Table

Local Temp Table

Local temp tables are only available to the current connection for the user; and they are automatically deleted when the user disconnects from instances. Local temporary table name is stared with hash ("#") sign.

Global Temp Table

Global Temporary tables name starts with a double hash ("##"). Once this table has been created by a connection, like a permanent table it is then available to any user by any connection. It can only be deleted once all connections have been closed.

Creating Temporary Table in SQL Server 2005

As I have already discussed, there are two types of temporary tables available. Here I am going to describe each of them.

Local Temporary Table

The syntax given below is used to create a local Temp table in SQL Server 2005:

CREATE TABLE #LocalTempTable(
UserID int,
UserName varchar(50), 
UserAddress varchar(150))

The above script will create a temporary table in tempdbdatabase. We can insertor deleterecords in the temporary table similar to a general table like:

insert into #LocalTempTable values ( 1, 'Abhijit','India');

Now selectrecords from that table:

select * from #LocalTempTable

After execution of all these statements, if you close the query window and again execute "Insert" or "Select" Command, it will throw the following error:

Msg 208, Level 16, State 0, Line 1
Invalid object name '#LocalTempTable'.

This is because the scope of Local Temporary table is only bounded with the current connection of current user.

Global Temporary Table

The scope of Global temporary table is the same for the entire user for a particular connection. We need to put "##" with the name of Global temporary tables. Below is the syntax for creating a Global Temporary Table:  

CREATE TABLE ##NewGlobalTempTable(
UserID int,
UserName varchar(50), 
UserAddress varchar(150))

The above script will create a temporary table in tempdb database. We can insertor deleterecords in the temporary table similar to a general table like:

insert into ##NewGlobalTempTable values ( 1, 'Abhijit','India');

Now select records from that table:

select * from ##NewGlobalTempTable

Global temporary tables are visible to all SQL Server connections. When you create one of these, all the users can see it.

Storage Location of Temporary Table

Temporary tables are stored inside the Temporary Folder of tempdb. Whenever we create a temporary table, it goes to Temporary folder of tempdb database.

TempTableLocation.JPG

Now, if we deeply look into the name of Local Temporary table names, a 'dash' is associated with each and every table name along with an ID. Have a look at the image below:

LocalTable.JPG - Click to enlarge image

SQL server does all this automatically, we do not need to worry about this; we need to only use the table name.  

When to Use Temporary Tables?

Below are the scenarios where we can use temporary tables:

  • When we are doing large number of row manipulation in stored procedures.
  • This is useful to replace the cursor. We can store the result set data into a temp table, then we can manipulate the data from there.
  • When we are having a complex joinoperation.

Points to Remember Before Using Temporary Tables

  • Temporary table created on tempdbof SQL Server. This is a separate database. So, this is an additional overhead and can causes performance issues.
  • Number of rows and columns need to be as minimum as needed.
  • Tables need to be deleted when they are done with their work.

Alternative Approach: Table Variable

Alternative of Temporary table is the Table variable which can do all kinds of operations that we can perform in Temptable. Below is the syntax for using Table variable.

Declare @TempTableVariable TABLE(
UserID int,
UserName varchar(50), 
UserAddress varchar(150))

The below scripts are used to insertand read the records for Tablevariables:

insert into @TempTableVariable values ( 1, 'Abhijit','India');

Now select records from that tablevariable:

select * from @TempTableVariable

When to Use Table Variable Over Temp Table

Tablevariable is always useful for less data. If the result set returns a large number of records, we need to go for temp table.

Reference and Further Study 

History

  • 23rd September, 2009: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Abhijit Jana
Technical Lead
India India
.NET Consultant | Former Microsoft MVP - ASP.NET | CodeProject MVP, Mentor, Insiders| Technology Evangelist | Author | Speaker | Geek | Blogger | Husband

Blog : http://abhijitjana.net
Web Site : http://dailydotnettips.com
Twitter : @AbhijitJana
My Kinect Book : Kinect for Windows SDK Programming Guide

You may also be interested in...

Comments and Discussions

 
GeneralGood Pin
venkatakumard11-Nov-09 2:26
membervenkatakumard11-Nov-09 2:26 
GeneralRe: Good Pin
Abhijit Jana11-Nov-09 5:08
mvpAbhijit Jana11-Nov-09 5:08 
GeneralGood Pin
Md. Marufuzzaman25-Sep-09 19:02
groupMd. Marufuzzaman25-Sep-09 19:02 
GeneralRe: Good Pin
Abhijit Jana26-Sep-09 0:44
mvpAbhijit Jana26-Sep-09 0:44 
GeneralNice article Champ!! Pin
Brij24-Sep-09 4:07
memberBrij24-Sep-09 4:07 
GeneralRe: Nice article Champ!! Pin
Abhijit Jana24-Sep-09 4:08
mvpAbhijit Jana24-Sep-09 4:08 
GeneralGood Article Pin
crudeCodeYogi24-Sep-09 3:58
membercrudeCodeYogi24-Sep-09 3:58 
GeneralRe: Good Article Pin
Abhijit Jana24-Sep-09 4:03
mvpAbhijit Jana24-Sep-09 4:03 
GeneralThere are more than 2 types of temporary table Pin
toticow23-Sep-09 22:56
membertoticow23-Sep-09 22:56 
GeneralRe: There are more than 2 types of temporary table Pin
Abhijit Jana23-Sep-09 23:00
mvpAbhijit Jana23-Sep-09 23:00 
GeneralRe: There are more than 2 types of temporary table Pin
toticow23-Sep-09 23:04
membertoticow23-Sep-09 23:04 
GeneralRe: There are more than 2 types of temporary table Pin
Abhijit Jana23-Sep-09 23:07
mvpAbhijit Jana23-Sep-09 23:07 
GeneralAny other table types Pin
jawaharraj8920-Mar-12 23:13
memberjawaharraj8920-Mar-12 23:13 
GeneralWow Pin
Abhishek Sur22-Sep-09 22:22
memberAbhishek Sur22-Sep-09 22:22 
Another awesome work.

I always love to see the efforts you spend on creating lovely graphic images on each of your articles.
This is simply the best. .. Smile | :) Smile | :)

5 from me.. .Thumbs Up | :thumbsup:

Abhishek Sur

My Latest Articles
Create CLR objects in SQL Server 2005
C# Uncommon Keywords
Read/Write Excel using OleDB

Don't forget to click "Good Answer" if you like to.

GeneralRe: Wow Pin
Abhijit Jana22-Sep-09 22:24
mvpAbhijit Jana22-Sep-09 22:24 
GeneralGood Work Again!! Pin
vivek_viv22-Sep-09 18:13
membervivek_viv22-Sep-09 18:13 
GeneralRe: Good Work Again!! Pin
Abhijit Jana22-Sep-09 18:32
mvpAbhijit Jana22-Sep-09 18:32 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150804.4 | Last Updated 22 Sep 2009
Article Copyright 2009 by Abhijit Jana
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid