SQL Server – If temp table exists – Drop it!

Send Us a Sign! (Contact Us!)
--> (Word) --> (PDF) --> (Epub) --> (Text)
--> (XML) --> (OpenOffice) --> (XPS)

You’ve created a temp table in a [gs stored procedure] and now you are testing it which involves dropping a temp table manually if you are not executing the stored procedure in it’s entirety (common if you are testing chunks of it).

So here’s the easy solution. We need to check if the temp table exists within the TempDB database and if it does, we need to drop it.

Place the following at the top of your procedure, obviously before creating the temp table =] Now everytime it runs it will drop the table and then recreate it, no more manually dropping the temp table!

IF OBJECT_ID('tempdb..#Temp') IS NOT NULL
DROP TABLE #Temp

Replace #Temp with the name of your temp table.

Hope this helps!

SOURCE

LINK (Lordfu.wordpress.com)

LANGUAGE
ENGLISH