<p>Why can't you do this and is there are work around?</p>

<p>You get this error.</p>

<p>Msg 2714, Level 16, State 1, Line 13
There is already an object named '#temptable' in the database.</p>

<pre><code>declare @x int

set @x = 1

if (@x = 0)
begin
    select 1 as Value into #temptable
end
else
begin
   select 2 as Value into #temptable
end

select * from #temptable

drop table #temptable
</code></pre>

[{'answer_id': 46527, 'author': 'Craig', 'author_id': 2894, 'author_profile': 'https://Stackoverflow.com/users/2894', 'pm_score': 0, 'selected': False, 'text': "<p>I am going to guess that the issue is that you haven't created the #temptable.</p>\n\n<p>Sorry I can't be more detailed but since you haven't even tried to explain what you are seeing you get a less than stellar answer.</p>\n"}, {'answer_id': 46530, 'author': 'SQLMenace', 'author_id': 740, 'author_profile': 'https://Stackoverflow.com/users/740', 'pm_score': 1, 'selected': False, 'text': "<p>You can't do that because of deferred name resolution, you can do it with a real table, just take out the pound signs</p>\n\n<p>You could also create the temp table first on top and then do a regular insert into table</p>\n"}, {'answer_id': 46539, 'author': 'Andrew', 'author_id': 826, 'author_profile': 'https://Stackoverflow.com/users/826', 'pm_score': 0, 'selected': False, 'text': "<p>From the look of the code is seems like you might have been prototyping this in SQL Studio or similiar, right? Can I guess that you've run this a few times and had it get to the point where it's created #temptable but then failed before it got to the end and dropped the table again? Restart the SQL editing tool you're using and try again.</p>\n"}, {'answer_id': 46565, 'author': 'Kevin Fairchild', 'author_id': 3743, 'author_profile': 'https://Stackoverflow.com/users/3743', 'pm_score': 1, 'selected': False, 'text': "<p>First step... check if the table already exists... if it does, delete it.  Next, explicitly create the table rather than using SELECT INTO...</p>\n\n<p>You'll find it much more reliable that way.</p>\n\n<pre><code>IF OBJECT_ID('tempdb..#temptable', 'U') IS NOT NULL \nBEGIN\nDROP TABLE #temptable\nEND\n\nCREATE TABLE #temptable (Value INT)\n\ndeclare @x int\n\nset @x = 1\n\nif (@x = 0)\nbegin\n    INSERT INTO #temptable (Value) select 1\nend\nelse\nbegin\n    INSERT INTO #temptable (Value) select 2\nend\n\nselect * from #temptable\n\ndrop table #temptable\n</code></pre>\n\n<p>Also, hopefully the table and field names are simplified for your example and aren't what you <em>really</em> call them ;)</p>\n\n<p>-- Kevin Fairchild</p>\n"}, {'answer_id': 46575, 'author': 'SQLMenace', 'author_id': 740, 'author_profile': 'https://Stackoverflow.com/users/740', 'pm_score': 1, 'selected': False, 'text': '<p>Deferred name resolution is also the reason you cannot be sure that sp_depends gives back correct results, check out this post I wrote a while back <a href="http://sqlblog.com/blogs/denis_gobo/archive/2008/05/06/6653.aspx" rel="nofollow noreferrer">Do you depend on sp_depends (no pun intended)</a></p>\n'}, {'answer_id': 147571, 'author': 'Peter Wone', 'author_id': 1715673, 'author_profile': 'https://Stackoverflow.com/users/1715673', 'pm_score': 2, 'selected': True, 'text': "<p>This is a two-part question and while Kev Fairchild provides a good answer to the second question he totally ignores the first - <em>why is the error produced?</em></p>\n\n<p>The answer lies in the way the preprocessor works. This</p>\n\n<pre><code>SELECT field-list INTO #symbol ...\n</code></pre>\n\n<p>is resolved into a parse-tree that is directly equivalent to </p>\n\n<pre><code>DECLARE #symbol_sessionid TABLE(field-list)\nINSERT INTO #symbol_sessionid SELECT field-list ...\n</code></pre>\n\n<p>and this puts #symbol into the local scope's name table. The business with _sessionid is to provide each user session with a private namespace; if you specify two hashes (##symbol) this behaviour is suppressed. Munging and unmunging of the sessionid extension is (ovbiously) transparent.</p>\n\n<p>The upshot of all this is that multiple INTO #symbol clauses produce multiple declarations in the same scope, leading to Msg 2714.</p>\n"}]
['https://Stackoverflow.com/questions/46495', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2184/']