Welcome to dbFreaks.com!
FAQFAQ    SearchSearch      ProfileProfile    Private MessagesPrivate Messages   Log inLog in

Detecting an existence of local temporary table

 
   Database Help (Home) -> Programming RSS
Next:  Datatype  
Author Message
Libor Forejtnik

External


Since: Feb 23, 2005
Posts: 3



(Msg. 1) Posted: Wed Feb 23, 2005 2:40 am
Post subject: Detecting an existence of local temporary table
Archived from groups: microsoft>public>sqlserver>programming (more info?)

Hi,

I create a temporary table. for example:

  create table #t1 (ID_T1 integer)

Next I would like to detect If the table already exists:

exec sp_table #t1
if @@ROWCOUNT > 0
  --Table #t1 exists
else
  --Table #t1 doesn't exist

But the sp_table stored proc.works only for non-temporary tables and
the result using temporary tables is allways @@ROWCOUNT = 0.

Does anyone know, if there exists a solution to detect an existing
temporary table ?

Thanks a lot for any suggestions

Libor

 >> Stay informed about: Detecting an existence of local temporary table 
Back to top
Login to vote
madhivanan2001

External


Since: Feb 22, 2005
Posts: 4



(Msg. 2) Posted: Wed Feb 23, 2005 2:40 am
Post subject: Re: Detecting an existence of local temporary table [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

If exists(Select name from sysobjects where name='#t1')
--Table #t1 exists
else
--Table #t1 doesn't exist


Madhivanan

 >> Stay informed about: Detecting an existence of local temporary table 
Back to top
Login to vote
Tibor Karaszi

External


Since: Jan 29, 2004
Posts: 1559



(Msg. 3) Posted: Wed Feb 23, 2005 3:40 am
Post subject: Re: Detecting an existence of local temporary table [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Temp tables are special. You can't just check against sysobjects, and you can't just check the
object id locally (as they live in tempdb). Try below:

IF OBJECT_ID('tempdb..#t') IS NOT NULL
PRINT 'exists'
ELSE
PRINT 'not exists'


--
Tibor Karaszi, SQL Server MVP
<a rel="nofollow" style='text-decoration: none;' href="http://www.karaszi.com/sqlserver/default.asp" target="_blank">http://www.karaszi.com/sqlserver/default.asp</a>
<a rel="nofollow" style='text-decoration: none;' href="http://www.solidqualitylearning.com/" target="_blank">http://www.solidqualitylearning.com/</a>


"Libor Forejtnik" wrote in message

 > Hi,
 >
 > I create a temporary table. for example:
 >
 > create table #t1 (ID_T1 integer)
 >
 > Next I would like to detect If the table already exists:
 >
 > exec sp_table #t1

 > --Table #t1 exists
 > else
 > --Table #t1 doesn't exist
 >
 > But the sp_table stored proc.works only for non-temporary tables and

 >
 > Does anyone know, if there exists a solution to detect an existing
 > temporary table ?
 >
 > Thanks a lot for any suggestions
 >
 > Libor
 >> Stay informed about: Detecting an existence of local temporary table 
Back to top
Login to vote
Uri Dimant

External


Since: Aug 24, 2003
Posts: 1744



(Msg. 4) Posted: Wed Feb 23, 2005 3:40 am
Post subject: Re: Detecting an existence of local temporary table [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

I'd prefer to use
IF object_id('TableName')IS NOT NULL
PRINT 'Yes'
ELSE
PRINT 'No'




wrote in message

 >
 > If exists(Select name from sysobjects where name='#t1')
 > --Table #t1 exists
 > else
 > --Table #t1 doesn't exist
 >
 >
 > Madhivanan
 >
 >> Stay informed about: Detecting an existence of local temporary table 
Back to top
Login to vote
Libor Forejtnik

External


Since: Feb 23, 2005
Posts: 3



(Msg. 5) Posted: Wed Feb 23, 2005 4:40 am
Post subject: Re: Detecting an existence of local temporary table [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

On 22 Feb 2005 23:04:52 -0800, wrote:

 >
 >If exists(Select name from sysobjects where name='#t1')
 > --Table #t1 exists
 >else
 > --Table #t1 doesn't exist
 >
 >
 >Madhivanan


Yes, the query should be exactlyt:

if exists(select * from tempdb..sysobjects where name like '#t1%')
..
..

but such query cannot be used for this purpose: while one session
creates a #t1 table and then other session performs such query
without creating is's own #t1 table, the result is allways more
than 0 rows.

Somewhere on server resides a relationship between a record of
tempdb..sysobject with name like '#t1%' and the user session. I tried
to recover such relationship, but (so far) I was unsuccessfull.

Libor
 >> Stay informed about: Detecting an existence of local temporary table 
Back to top
Login to vote
Libor Forejtnik

External


Since: Feb 23, 2005
Posts: 3



(Msg. 6) Posted: Wed Feb 23, 2005 4:40 am
Post subject: Re: Detecting an existence of local temporary table [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

On Wed, 23 Feb 2005 08:59:44 +0100, "Tibor Karaszi"
wrote:

 >Temp tables are special. You can't just check against sysobjects, and you can't just check the
 >object id locally (as they live in tempdb). Try below:
 >
 >IF OBJECT_ID('tempdb..#t') IS NOT NULL
 >PRINT 'exists'
 >ELSE
 >PRINT 'not exists'

This works very good. Thanks

Libor
 >> Stay informed about: Detecting an existence of local temporary table 
Back to top
Login to vote
Display posts from previous:   
Related Topics:
existence of temp table - How do you check for the existence of a temp table before removing it with drop? thanks!

using temporary table - Hello there I need to place on temporary table some data which isn't unique If i'm using select into statement it works the fastest way. If i only create table by Create table statement without declaring and primary key it works little slower but ok....

temporary table? - Hi , I'm not sure how to do this in a sp. I need to get a value from a table to feed into a another query, something along the lines of: Select type from tbltype where caseno = @caseno and seqno =@seqno these will be parameters used throughout the sp...

Temporary Table Problem - Hi, I have an stored procedure wherein I am creating an temporary table, populating data onto that and finally the output will be resultset from the temporary table. The structure for the temporary table is generated based on the parameters specified....

Create a temporary table within a view - Is there a way to create a temporary table within a view. Using a view I should list the months between a date range and display the Month and Year. I have a table with a start date and end date. I need to create a view and display the table data with...
   Database Help (Home) -> Programming All times are: Pacific Time (US & Canada)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You can edit your posts in this forum
You can delete your posts in this forum
You can vote in polls in this forum



[ Contact us | Terms of Service/Privacy Policy ]