October 12, 2007 at 6:59 am
Just a pre-coffee question but are tables defined as #temp unique to a particular instance of being called?
Say I have a stored procedure that builds up a #temp table, do I have to worry that two different users running the same proc will trip over one another?
I had thought that when you created a #temp table that it actually has a unique internal name but I can't find anything on it in particular.
thanks,
Chris
October 12, 2007 at 7:07 am
The scope of the temp table is local to the stored procedure and it will not create any multi-user conflicts if the same procedure is accessed by mutlitple users at the same time. However it is a good standard to use drop temp tables after you have completed using them inside to procedure even though the scope of the temp table would end after the execution of procedure is completed.
Prasad Bhogadi
www.inforaise.com
October 12, 2007 at 7:09 am
local temptables have the scope of a connection, the name SQL Server creates for them in tempdb is postfixed with undescores and some number.
Regards,
Andras
October 12, 2007 at 7:12 am
to add to what Prasad said, you can see the results for yourself. create a temp table, and peak at tempdb: a table unique to your session gets created...it may start with the spelling you made, but a LOT of undersores, and then a uniqueid is appended to teh end of it.
that way, if the a different user creates a temp table with the same name, it's still unique.
you can make a "global" temp table with double pound signs, Create table ##tmp, and there will only be one, which can be accessed accross sessions.
create table #tmp(id int)
select * from tempdb.dbo.sysobjects where name like '#tmp%'
#tmp__[108 more underscores]__00000000116F
Lowell
October 12, 2007 at 8:39 am
If you are concerned about whether #tables are unique (and I think that they are), then use table variables instead.
There are a few differences such as you have to declare the table variable explicitly rather than creating it with a select into and you cannot index table variables (other people will point out other differences I'm sure).
Just an idea.
Jez
October 12, 2007 at 8:50 am
As one of those other people who will point out some of the differences, I suggest you have a look at http://www.sql-server-performance.com/articles/per/temp_tables_vs_variables_p1.aspx
Under 2005 the performance of table variables for large tables is worth keeping a close eye on.
Regards,
Andras
October 12, 2007 at 9:09 am
Andras,
Are you suggesting that #temp tables are preferable to table variables?
Jez
October 12, 2007 at 9:14 am
Both of them have their places. But do not store much data in table variables. Not only they suffer from the above performance problems, you cannot create non-clustered indexes on them, no statistics, etc. They are however created in the tempdb the same way as temp tables.
Have a look at http://support.microsoft.com/default.aspx?scid=kb;en-us;305977&Product=sql2k
Regards,
Andras
October 12, 2007 at 9:22 am
Thanks.
I hadn't realised that it was possible to create constraints (PK, unique) on table variables so that's something else I learnt today.
Jez
October 12, 2007 at 9:34 am
Thanks for the replies!
October 12, 2007 at 9:26 pm
Christopher Klein (10/12/2007)
Just a pre-coffee question but are tables defined as #temp unique to a particular instance of being called?Say I have a stored procedure that builds up a #temp table, do I have to worry that two different users running the same proc will trip over one another?
I had thought that when you created a #temp table that it actually has a unique internal name but I can't find anything on it in particular.
thanks,
Chris
I know folks have already answered, but thought I'd throw my 2 cents in, anyway...
Yes, temp tables are unique between sessions.
No, you do not have to worrty that two different users are running the same proc.
All bets are off if you use a "Global" temp table.
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 11 posts - 1 through 10 (of 10 total)
You must be logged in to reply to this topic. Login to reply