I'm trying to create a temp table dependent on the value of a scenario parameter and using the following IF statement but getting the error below:
IF @indexName = 'A'
begin select top 400 * into #temp from #pretemp order by EMRev desc end
ELSE IF @indexName = 'B'
begin select top 75 * into #temp from #pretemp order by EMRev desc end
ELSE IF @indexName = 'C'
begin select top 300 * into #temp from #pretemp order by EMRev desc end
ELSE
begin select top 100 * into #temp from #pretemp order by EMRev desc end
Msg 2714, Level 16, State 1, Line 179 There is already an object named '#temp' in the database. Msg 2714, Level 16, State 1, Line 181 There is already an object named '#temp' in the database. Msg 2714, Level 16, State 1, Line 183 There is already an object named '#temp' in the database.
I'm certain the IF statement works based on the @indexName variable (replacing the block statement with something simple (eg, 'select @indexName'), the program runs fine).
Any ideas about what's causing this error?