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?
If you are getting an error about the else it is because you've told the interpreter that the ; was the end of your if statement so when it finds the else a few lines later it starts complaining. A few examples of where not to put a semicolon: if (age < 18); if ( 9 > 10 ); if ("Yogi Bear".
Use an if/else statement if the two conditions are mutually exclusive meaning if one condition is true the other condition must be false. if (testScore > 60) cout << "You pass" << endl; if (testScore > 90) cout << "You did great" << endl; For example, before noon (AM) and after noon (PM) are mutually exclusive.
If the if-statement is True , the code section under the else clause does not run. The keyword else needs to be on its own line and be at the same indentation level as the if keyword that the else corresponds to. The keyword else needs to be followed by a colon : .
In general, "else if" style can be faster because in the series of ifs, every condition is checked one after the other; in an "else if" chain, once one condition is matched, the rest are bypassed.
The SQL parser looks for all places that might create a temp table, and only allows a given temp table name to be used ONE time. The solution is to do something like this:
select * into #temp from #pretemp where 1=2
IF @indexName = 'A'
begin insert into #temp select top 400 * from #pretemp order by EMRev desc end
ELSE IF @indexName = 'B'
begin insert into #temp select top 75 * from #pretemp order by EMRev desc end
ELSE IF @indexName = 'C'
begin insert into #temp select top 300 * from #pretemp order by EMRev desc end
ELSE
begin insert into #temp select top 100 * from #pretemp order by EMRev desc end
The where 1=2
creates the table structure with zero records... Then the if statements populate the temp table.
Cheers!
Since only the top number of records are difference. You can try this
declare @num int
SET @num = CASE @indexName
WHEN 'A' THEN 400
WHEN 'B' THEN 75
WHEN 'C' THEN 300
ELSE 100
END
select top (@num) * into #temp from #pretemp order by EMRev desc
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With