Using EXISTS to find a temporary table

  • I'm working on a stored procedure which uses temp tables. Since I often get things wrong multiple times before they work, I frequently have the procedure fail before it gets to the part where it drops the temp table. Is there a way to use EXISTS to search to see if a temp table (which begins with the '#' character and is stored in the tempdb) already exists in the system so I can drop it before creating a new one? Otherwise I get an error and have to go back and manually drop the table before I can run the query.

    Thanks!

  • If Object_ID('TempDb..#Temp') is not NULL Drop Table #Temp

    Or in some cases...

    If Object_ID('TempDb..#Temp') is not NULL Exec ('Drop Table #Temp')



    Once you understand the BITs, all the pieces come together

  • You rule. Thanks!

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

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