﻿<?xml version='1.0' encoding='UTF-8'?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>SQLServerCentral / Article Discussions / Article Discussions by Author / Discuss content posted by Mike Arney  / One line to drop a (temp) table if it exists / Latest Posts</title><generator>InstantForum.NET v2.9.0</generator><description>SQLServerCentral</description><link>http://www.sqlservercentral.com/Forums/</link><webMaster>notifications@sqlservercentral.com</webMaster><lastBuildDate>Sat, 25 May 2013 15:56:59 GMT</lastBuildDate><ttl>20</ttl><item><title>RE: One line to drop a (temp) table if it exists</title><link>http://www.sqlservercentral.com/Forums/Topic581724-1315-1.aspx</link><description>[quote][b]Mike Arney (4/15/2009)[/b][hr]First, thanks for the comment about object_id('tempdb..#t'): I did not realize that would reliably work.  I was afraid if 2 sessions both had a #t, object_id() would have the same problem as something like "select * from tempdb.sys.objects where name like '#t%' ", namely, it would not distinguish between the different #t's in the different sessions.  Seems that it does that just fine.[/quote]That's because, behind the scenes, the local temp table isn't named simply "#t".  It'll be something like...#t_________________________________________________________________________________________________________12A52C39F0That's what allows local temp table to be session specific and also prevents mutual interfernce between sessions that all want to use a #t table.</description><pubDate>Thu, 16 Apr 2009 12:01:37 GMT</pubDate><dc:creator>Jeff Moden</dc:creator></item><item><title>RE: One line to drop a (temp) table if it exists</title><link>http://www.sqlservercentral.com/Forums/Topic581724-1315-1.aspx</link><description>First, thanks for the comment about object_id('tempdb..#t'): I did not realize that would reliably work.  I was afraid if 2 sessions both had a #t, object_id() would have the same problem as something like "select * from tempdb.sys.objects where name like '#t%' ", namely, it would not distinguish between the different #t's in the different sessions.  Seems that it does that just fine.As far as the catch not working due to insufficiently severe errors, the drop of a non-existent table raises a severity 11 error.  According to BOL (topic "TRY...CATCH"): "A TRY…CATCH construct catches all execution errors that have a severity higher than 10 that do not close the database connection.".  So these errors are not caught:[code]begin try raiserror('severity 10 error, will NOT be caught', 10, 1); end try begin catch select 'catching'; end catch;begin try print 'this print will print'; end try begin catch select 'catching'; end catch;[/code]... but this will be caught:[code]begin try raiserror('higher severity error, will be caught', 11, 1); end try begin catch select 'catching'; end catch;[/code]Thanks everyone for your comments.</description><pubDate>Wed, 15 Apr 2009 08:31:30 GMT</pubDate><dc:creator>Mike Arney</dc:creator></item><item><title>RE: One line to drop a (temp) table if it exists</title><link>http://www.sqlservercentral.com/Forums/Topic581724-1315-1.aspx</link><description>I think I recall failing to get a try/catch block to work (but likely not with a #temp table) because (I thought) the error returned was not at a high enough level.Any idea what I might have tripped over.</description><pubDate>Wed, 15 Apr 2009 07:19:03 GMT</pubDate><dc:creator>Jim Russell-390299</dc:creator></item><item><title>RE: One line to drop a (temp) table if it exists</title><link>http://www.sqlservercentral.com/Forums/Topic581724-1315-1.aspx</link><description>Seems like a nifty trick but I prefer the old fashioned way:IF OBJECT_ID('tempdb..#t') IS NOT NULL DROP TABLE #t;</description><pubDate>Tue, 14 Apr 2009 11:18:09 GMT</pubDate><dc:creator>tfountain</dc:creator></item><item><title>RE: One line to drop a (temp) table if it exists</title><link>http://www.sqlservercentral.com/Forums/Topic581724-1315-1.aspx</link><description>Hi Mike,Thanks for this tip.Didn't know Sql now has a Try / Catch.Happy New Year !Regards, Paul</description><pubDate>Fri, 26 Dec 2008 12:23:04 GMT</pubDate><dc:creator>Paul Chu</dc:creator></item><item><title>One line to drop a (temp) table if it exists</title><link>http://www.sqlservercentral.com/Forums/Topic581724-1315-1.aspx</link><description>Comments posted to this topic are about the item [B]&lt;A HREF="/scripts/T-SQL/64559/"&gt;One line to drop a (temp) table if it exists&lt;/A&gt;[/B]</description><pubDate>Tue, 07 Oct 2008 06:23:29 GMT</pubDate><dc:creator>Mike Arney</dc:creator></item></channel></rss>