Using T-SQL to check whether a global temp table exists

  • Hi

    How to check using T-SQL whether a global temp table exists ?

    Any one help me ????

    :):):)

    thanks

    venkat

  • I got it...

    DECLARE @temp_table VARCHAR(100)

    SET @temp_table = '##my_temp_table'

    IF NOT EXISTS (SELECT 'x'

    FROM tempdb..sysobjects

    WHERE type = 'U' and NAME = @temp_table)

    PRINT 'temp table ' + @temp_table + ' does not exist'

    ELSE

    PRINT 'temp table ' + @temp_table + ' exists.'

    thanks

  • IF OBJECT_ID('TempDB..##my_temp_table','U') IS NOT NULL

    PRINT 'Table Exists'

    ELSE

    PRINT 'Table does not Exist'

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply