Results 1 to 5 of 5
  1. #1
    Join Date
    Apr 2003
    Location
    Phoenix, AZ
    Posts
    177

    Question Unanswered: How to check existance of temp table

    Within the execution of a t-sql script how do I check for the existance of a temporary table associated with the session or a global temporary table?

    My understanding is that the table name doesn't get placed in the current database sysobjects table - it goes into tempdb. But the object name is cryptic (so as to be unique) and I see no way of associating it with the current session (@@SPID).

    Thanks,
    Fred
    Fred Prose

  2. #2
    Join Date
    Nov 2002
    Location
    Jersey
    Posts
    10,322
    Local temp tables, yes, not global

    Code:
    CREATE TABLE ##myTemp99(Col1 int)
    GO
    
    SELECT * FROM tempdb..sysobjects WHERE Name = '##myTemp99' 
    GO
    
    DROP TABLE ##myTemp99
    GO
    Brett
    8-)

    It's a Great Day for America everybody!

    dbforums Yak CorralRadio 'Rita
    dbForums Member List
    I'm Good Once as I ever was

    The physical order of data in a database has no meaning.

  3. #3
    Join Date
    Jul 2004
    Posts
    52
    You can also do:

    If Object_ID('tempdb..#mytemp') Is NULL
    ... table does not exist

  4. #4
    Join Date
    Nov 2002
    Location
    Jersey
    Posts
    10,322
    Very cool...cut and paste this in to as many seesions as you want...

    It's isolated to that session...

    Code:
    USE Northwind
    GO
    
    CREATE TABLE #myTemp99(Col1 int)
    
    SELECT Object_ID('tempdb..#mytemp99')
    GO
    
    DROP TABLE #myTemp99
    GO
    Brett
    8-)

    It's a Great Day for America everybody!

    dbforums Yak CorralRadio 'Rita
    dbForums Member List
    I'm Good Once as I ever was

    The physical order of data in a database has no meaning.

  5. #5
    Join Date
    Oct 2003
    Posts
    84
    I don't know if this method is failsafe but it's the one i usually use...


    if OBJECT_ID('TempDB..#MyTempTableName') IS NOT NULL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •