Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Odd error using IF/ELSE IF statements

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?

like image 330
Chris Avatar asked Jun 28 '12 23:06

Chris


People also ask

Why is my if-else statement not working?

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".

What is an example of an if-else statement?

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.

Why is my if-else statement not working Python?

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 : .

Which is better if-else or if Elseif?

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.


2 Answers

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!

like image 131
James L. Avatar answered Nov 13 '22 14:11

James L.


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
like image 26
EricZ Avatar answered Nov 13 '22 13:11

EricZ