⌘BABAKOV.NET Let's code!
100% [light] [ru]

Deleting Temporary Tables in MSSQL

Sometimes there is a need to delete a temporarily created table during a procedure, which may or may not exist. Simply writing, for example, drop table #myTempTable is not enough, because if the table does not exist, the query will not execute and will return an error. The example below shows how to correctly delete temporary tables in such cases:

if exists (
	select * from tempdb.dbo.sysobjects o
	where o.xtype in (\'U\') 

	and o.id = object_id(N\'tempdb..#myTempTable\')
)
BEGIN
  DROP TABLE #myTempTable; 
END

Comments (0)

No comments yet.

Leave a comment
Comment
Images (up to 5 MB)
Your comment will appear after moderation.