IF OBJECT_ID('tempdb..#iftry') IS NOT NULL
DROP TABLE #iftry
IF OBJECT_ID('BIC_Analytics.dbo.AdjudicateAllDCCharteredClaims') IS NULL
begin
select 'this is start of first block'
SELECT 'this is first block' as blockid
INTO #iftry
select 'this is end of first block'
end
ELSE
begin
select 'this is start of 2nd block'
SELECT 'this is 2nd block' as blockid
INTO #iftry
select 'this is end of 2nd block'
end
select ':)'
select * from #iftry
Keeps giving me the error:
Msg 2714, Level 16, State 1, Line 18
There is already an object named '#iftry' in the database.
Now it works
IF OBJECT_ID('tempdb..#iftry') IS NOT NULL
DROP TABLE #iftry
create table #iftry (blockid varchar(20))
IF OBJECT_ID('BIC_Analytics.dbo.AdjudicateAllDCCharteredClaims') IS NOT NULL
begin
--select 'this is start of first block'
insert into #iftry (blockid)
select 'this is first block'
--select 'this is end of first block'
end
ELSE
begin
--select 'this is start of 2nd block'
insert into #iftry (blockid)
select 'this is 2nd block'
--select 'this is end of 2nd block'
end
select ':)'
select * from #iftry
This is a parsing problem, not a runtime problem. SQL Server can’t see that there are two code paths that can’t be reached.
To work around it, create your #temp table once up front:
There’s no way to tell SQL Server not to work this way unless you put the two #table declarations in separate batches (which you can’t really do), or build dynamic SQL and work with the #temp table at that scope (not fun).