T-SQL error object exists when separated in if/else blocks
Posted
by Jeff O
on Stack Overflow
See other posts from Stack Overflow
or by Jeff O
Published on 2010-04-08T14:37:21Z
Indexed on
2010/04/08
14:43 UTC
Read the original article
Hit count: 288
sql-server-2005
|tsql
I get the error: Msg 2714, Level 16, State 1, Line 16 There is already an object named '#mytemptable' in the database.
There are ways around it, but wonder why this happens. Seems like SQL Server is verifying both blocks of the if/else statement?
declare @choice int
select @choice = 1
if @choice = 1
begin
select 'MyValue = 1' AS Pick into #my_temp_table
end
else
begin
select 'MyValue <> 1' AS Pick into #my_temp_table
end
select * from #temptable
drop table #temptable
If the tables have different names, it works. Or if I create the temp table and use Insert Into... statements that works as well.
© Stack Overflow or respective owner