Home All Groups Group Topic Archive Search About

how to check if #temp table exists?

Author
14 Jul 2005 4:12 PM
Rich
I create a temp table

Create Table #temp...

and do stuff - without dropping #temp.  Then I tried using the following in
Query Analyzer

if exists (select * from dbo.sysobjects where id =
object_id(N'[dbo].[#temp]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[#temp]

and then

Create Table #temp...  again.  But I get the error message that #temp
already exists.  I am guessing that dbo is tempDB.  How do I reference temDB?
How can I detect if it exists so I can drop #temp?

Thanks,
Rich

Author
14 Jul 2005 4:21 PM
Aaron Bertrand [SQL Server MVP]
> But I get the error message that #temp already exists.

Why are you using temp tables?  Why wouldn't you know if the temp table
already exists?  If the table is of the same structure, why not just empty
the table and re-use it?  If it is not of the same structure, why not drop
it when you are done, so you can re-create it?  Or use different names for
different temp tables?

Anyway, the answer about how to determine if a temp table exists is here:
http://www.aspfaq.com/2458

> I am guessing that dbo is tempDB.

I have no idea what you mean.  dbo is the built in database owner.  tempDB
is a database.
Author
14 Jul 2005 4:25 PM
Alejandro Mesa
Try,

if object_id('tempdb..#temp') is not null
    print 'exists.'
else
   print 'does not exist.'


AMB


Show quote
"Rich" wrote:

> I create a temp table
>
> Create Table #temp...
>
> and do stuff - without dropping #temp.  Then I tried using the following in
> Query Analyzer
>
> if exists (select * from dbo.sysobjects where id =
> object_id(N'[dbo].[#temp]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
> drop table [dbo].[#temp]
>
> and then
>
> Create Table #temp...  again.  But I get the error message that #temp
> already exists.  I am guessing that dbo is tempDB.  How do I reference temDB?
>  How can I detect if it exists so I can drop #temp?
>
> Thanks,
> Rich
Author
14 Jul 2005 4:36 PM
David Gugick
Alejandro Mesa wrote:
> Try,
>
> if object_id('tempdb..#temp') is not null
>    print 'exists.'
> else
>   print 'does not exist.'
>

Very cool. I like how the function is smart enough to look for an owned
temporary table.

Here's a question: Where in sysobjects is stored the session that owns
the temp table? Is it just a part of the extended temp table object
name?

--
David Gugick
Quest Software
www.imceda.com
www.quest.com
Author
14 Jul 2005 6:23 PM
Alejandro Mesa
David,

No idea. The only thing I know is that sql server use a system-generated
numeric suffix that is appended to the table name. I do not know if this
number is unique for all temporary tables or if it is unique by users.


AMB


Show quote
"David Gugick" wrote:

> Alejandro Mesa wrote:
> > Try,
> >
> > if object_id('tempdb..#temp') is not null
> >    print 'exists.'
> > else
> >   print 'does not exist.'
> >
>
> Very cool. I like how the function is smart enough to look for an owned
> temporary table.
>
> Here's a question: Where in sysobjects is stored the session that owns
> the temp table? Is it just a part of the extended temp table object
> name?
>
> --
> David Gugick
> Quest Software
> www.imceda.com
> www.quest.com
>
>
Author
14 Jul 2005 4:57 PM
Rich
Thank you all for your replies.  Basically, I wanted to know how you check
for the existence of a temp table.   As I expected, you reference tempDB.  As
for why I would want to check for the existence of a #temp table - safety. 
If someone needs to rerun an SP where #temp is created (and dropped of
course) just making sure it was dropped before recreating it.

Show quote
"Rich" wrote:

> I create a temp table
>
> Create Table #temp...
>
> and do stuff - without dropping #temp.  Then I tried using the following in
> Query Analyzer
>
> if exists (select * from dbo.sysobjects where id =
> object_id(N'[dbo].[#temp]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
> drop table [dbo].[#temp]
>
> and then
>
> Create Table #temp...  again.  But I get the error message that #temp
> already exists.  I am guessing that dbo is tempDB.  How do I reference temDB?
>  How can I detect if it exists so I can drop #temp?
>
> Thanks,
> Rich
Author
14 Jul 2005 7:58 PM
Ross Presser
On Thu, 14 Jul 2005 09:57:04 -0700, Rich wrote:

> Thank you all for your replies.  Basically, I wanted to know how you check
> for the existence of a temp table.   As I expected, you reference tempDB.  As
> for why I would want to check for the existence of a #temp table - safety. 
> If someone needs to rerun an SP where #temp is created (and dropped of
> course) just making sure it was dropped before recreating it.

A different way to guard against that possibility is to use table variables
instead of temp tables.

AddThis Social Bookmark Button