<p>When writing a T-SQL script that I plan on re-running, often times I use temporary tables to store temporary data.  Since the temp table is created on the fly, I'd like to be able to drop that table only if it exists (before I create it).</p>

<p>I'll post the method that I use, but I'd like to see if there is a better way.</p>

[{'answer_id': 2652, 'author': 'Nathan Bedford', 'author_id': 434, 'author_profile': 'https://Stackoverflow.com/users/434', 'pm_score': 4, 'selected': False, 'text': '<p>The OBJECT_ID function returns the internal object id for the given object name and type.  \'tempdb..#t1\' refers to the table #t1 in the tempdb database. \'U\' is for user-defined table.</p>\n\n<pre class="lang-sql prettyprint-override"><code>IF OBJECT_ID(\'tempdb..#t1\', \'U\') IS NOT NULL\n  DROP TABLE #t1\n\nCREATE TABLE #t1\n(\n  id INT IDENTITY(1,1),\n  msg VARCHAR(255)\n)\n</code></pre>\n'}, {'answer_id': 2653, 'author': 'GateKiller', 'author_id': 383, 'author_profile': 'https://Stackoverflow.com/users/383', 'pm_score': 6, 'selected': True, 'text': "<pre><code>IF Object_Id('TempDB..#TempTable') IS NOT NULL\nBEGIN\n    DROP TABLE #TempTable\nEND\n</code></pre>\n"}, {'answer_id': 87536, 'author': 'Keith Maurino', 'author_id': 1096640, 'author_profile': 'https://Stackoverflow.com/users/1096640', 'pm_score': 0, 'selected': False, 'text': "<pre><code>SELECT name\nFROM sysobjects\nWHERE type = 'U' AND name = 'TempTable'\n</code></pre>\n"}]
['https://Stackoverflow.com/questions/2649', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/434/']