Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Temp Table Or Table Variable

Author  Topic 

karuna
Aged Yak Warrior

582 Posts

Posted - 2006-05-24 : 08:45:34
Where can I use Temp Table and where can I use Table Variable.
If data is needed in the table for some manipulation inside the stored proc, table variable makes sense for me.

Anybody can give me pointers on performance front? Also if two users access the sp at the same time, what will happen in the case temp table? coz, the first line of my sp is to drop the temp table if it exists.

Thanks


Karunakaran

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2006-05-24 : 08:56:23
from Books On Line on CREATE TABLE
quote:
Temporary Tables
You can create local and global temporary tables. Local temporary tables are visible only in the current session; global temporary tables are visible to all sessions.


2 users = 2 connection = 2 session. No problem.

You can use QA to try this out. Open 2 window and create temp table of same name in both window.


KH

Go to Top of Page

karuna
Aged Yak Warrior

582 Posts

Posted - 2006-05-24 : 09:09:59
quote:
Originally posted by khtan

from Books On Line on CREATE TABLE
quote:
Temporary Tables
You can create local and global temporary tables. Local temporary tables are visible only in the current session; global temporary tables are visible to all sessions.


2 users = 2 connection = 2 session. No problem.

You can use QA to try this out. Open 2 window and create temp table of same name in both window.


KH




Lets say #tmp is my temp table.
If I use the same object name in 2 different sessions,then it should create 2 #tmp objects, so Sql Server internally will give different alias / name for these 2 objects?





Karunakaran
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2006-05-24 : 09:17:11
Try to run this in 2 QA window
create table #temp (col1 int)
select * from tempdb..sysobjects where name like '#temp%'



KH

Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2006-05-24 : 09:31:17
If you use temp table in sp, it will be dropped after the scope is over
Also read this
http://support.microsoft.com/default.aspx?scid=kb;en-us;305977

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

Srinika
Master Smack Fu Yak Hacker

1378 Posts

Posted - 2006-05-24 : 10:37:48
I'm waiting to see the answer for :
quote:
Originally posted by karuna

Where can I use Temp Table and where can I use Table Variable.



I mean when to select one over the other

Srinika
Go to Top of Page

TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2006-05-24 : 10:58:38
>>Where can I use Temp Table and where can I use Table Variable.
Some ramplings:

If you are going to insert to the table the results of any exec'd statement than you can't use a table variable.

If your table will hold more than "a small amount" of data you will be better off with a temp table. Thousands of rows in a table variable can be extremely slow, especially joined to other tables.

If you want to access the table accross multiple batches (intra-GOs) or if you want to manually execute portions of code at a time then temp tables are the way to go. Table variables - like any local variable - need to be re-declared and initialized everytime they are used. Within a single session a temp table is available until you drop it.

Table variables used in SPs can reduce the activity in tempdb as well as reduce re-compiles.

One of the things I use table variables a lot for is things like holding parsed values sent in to an SP by a comma seperated string.

temp tables can be indexed or pretty much anything you can do with a permanent table (except things like RI)

Be One with the Optimizer
TG
Go to Top of Page

karuna
Aged Yak Warrior

582 Posts

Posted - 2006-05-24 : 14:47:24
Thanks for the input TG.
I dont have many rows, mostly less than 1000 at any given time.
I preferred table variable because I dont see any need for index, the tempdb log / transaction would be less.

Thanks

Karunakaran
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2006-05-25 : 22:58:03
Just came across this. Difference between temporary table @, #
Thought you might be interested.


KH

Go to Top of Page
   

- Advertisement -