Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

There a way to check if Temp Table exists? 1

Status
Not open for further replies.

nerdalert1

Programmer
Nov 4, 2004
92
US
I want to check if a #temp table exists at the start of a proc and then drop it if it does. Anyone know? Thanks all.
 
Yes there is. You have to check the sysobjects table in the tempdb database.
Code:
use master
go
if exists (select * from tempdb.dbo.sysobjects where name like '#test%')
    drop table #test
create table #test
(c1 int)
go
Now this doesn't account for more than one spid having a temp table with the same name. But I think you get the idea.

Denny

--Anything is possible. All it takes is a little research. (Me)

[noevil]
(My very old site)
 
A local #temp table cannot exist at the start of a stored procedure calling it. IT isn;t in scope until you creat it within the stored porcedure and each user running the procedure creates their own version. You only need to check for global temp tables (those starting with ##).

Of course table variables are generally more efficient, although they can do everything a temp table can.

Questions about posting. See faq183-874
 
SQLSister thats right. Thanks for the info. I forgot that.
 
SQLSister but variables stay in memory worst dont they?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top