{"id":2412,"date":"2012-08-30T21:57:31","date_gmt":"2012-08-31T04:57:31","guid":{"rendered":"https:\/\/sqlserverplanet.com\/?p=2412"},"modified":"2012-11-06T00:26:17","modified_gmt":"2012-11-06T00:26:17","slug":"drop-table-if-exists","status":"publish","type":"post","link":"https:\/\/sqlserverplanet.com\/tsql\/drop-table-if-exists","title":{"rendered":"Drop Table if Exists"},"content":{"rendered":"<p>To determine if a table exists, it&#8217;s best to go against the sys.objects view by querying the object_id using the fully qualified name of the table. The additional &#8216;type&#8217; field in the where clause ensures that the table that is about to be dropped is a User table and not a system table.<\/p>\n<div class=\"block style05\">\r\n\t\t\t\t\t\t<img decoding=\"async\" src=\"https:\/\/sqlserverplanet.com\/wp-content\/themes\/sqlserverplanet\/images\/ico14.gif\" alt=\"\" class=\"ico\" \/>\r\n\t\t\t\t\t\t<div class=\"text-holder\">\r\n\t\t\t\t\t\t\tUse caution when dropping tables. Once you drop a table, you will not be able to get it back unless you restore a backup.\r\n\t\t\t\t\t\t<\/div>\r\n\t\t\t\t\t<\/div>\n<p>[cc lang=&#8221;sql&#8221;]<br \/>\nIF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[DumpMe]&#8217;) AND type in (N&#8217;U&#8217;))<br \/>\nDROP TABLE [dbo].[DumpMe]<br \/>\n[\/cc]<\/p>\n<p>To drop a temp table you need to look in the tempdb database for it&#8217;s existence.<\/p>\n<p>[cc lang=&#8221;sql&#8221;]<br \/>\nIF OBJECT_ID(&#8216;tempdb..#Temp&#8217;) IS NOT NULL<br \/>\nBEGIN<br \/>\nDROP TABLE #Temp<br \/>\nEND<br \/>\n[\/cc]<\/p>\n<p>Once again, I would highly recommend double checking your code prior to issuing these statements.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>To determine if a table exists, it&#8217;s best to go against the sys.objects view by querying the object_id using the fully qualified name of the table. The additional &#8216;type&#8217; field in the where clause ensures that the table that is about to be dropped is a User table and not a system table. [cc lang=&#8221;sql&#8221;] IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[DumpMe]&#8217;) AND type in (N&#8217;U&#8217;)) DROP TABLE [dbo].[DumpMe] [\/cc] To drop a temp table you need to look in the tempdb database for it&#8217;s existence. [cc lang=&#8221;sql&#8221;] IF OBJECT_ID(&#8216;tempdb..#Temp&#8217;) IS NOT NULL BEGIN DROP TABLE #Temp END [\/cc] Once again, I would highly recommend double checking your code prior to issuing these statements.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_s2mail":"yes","footnotes":""},"categories":[34],"tags":[],"class_list":["post-2412","post","type-post","status-publish","format-standard","hentry","category-tsql"],"_links":{"self":[{"href":"https:\/\/sqlserverplanet.com\/wp-json\/wp\/v2\/posts\/2412","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sqlserverplanet.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sqlserverplanet.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sqlserverplanet.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/sqlserverplanet.com\/wp-json\/wp\/v2\/comments?post=2412"}],"version-history":[{"count":8,"href":"https:\/\/sqlserverplanet.com\/wp-json\/wp\/v2\/posts\/2412\/revisions"}],"predecessor-version":[{"id":2675,"href":"https:\/\/sqlserverplanet.com\/wp-json\/wp\/v2\/posts\/2412\/revisions\/2675"}],"wp:attachment":[{"href":"https:\/\/sqlserverplanet.com\/wp-json\/wp\/v2\/media?parent=2412"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sqlserverplanet.com\/wp-json\/wp\/v2\/categories?post=2412"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sqlserverplanet.com\/wp-json\/wp\/v2\/tags?post=2412"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}