Check if temporary table exists or not?

  • But it is still a great question, how would one query to see if the session/temporary table exists. The table name in cases when it is named #temptable is really something like "#temptable_______________...2345626C" in the temp database. So, querying objects table would not return any results if the name is just named #temptable, but ##temptable will, because MSSQL maintains the name. Anybody have any great ideas?

  • Just trust it -- if you create a #temptable, it will be there until the end of your script/session. If you create a new query to test if it worked, it will seem it didn't, because your new query is not part of the same session. But if you create it within some lower level stored procedure it will not survive back to the calling level.

    If you create a ##temptable, it will persist until the last session using it ends...but if you have a real need for a psudo-global table like that I'd like to hear about it. To me it makes no sense at all.

  • Hi

    1. Check if temporary table exists...

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

    PRINT 'Temp Table Exists'

    ELSE

    PRINT 'Temp Table Doesnt Exists'

    2. Same for permanent tables...

    IF OBJECT_ID('dbo.Table_name','U') IS NOT NULL

    PRINT 'Temp Table Exists'

    ELSE

    PRINT 'Temp Table Doesnt Exists'

    Thanks -- Vj

  • Hi

Viewing 4 posts - 46 through 48 (of 48 total)

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