2016-05-06 8 views
1

Wie Bedingungen innerhalb Case-Anweisung verwenden:Zustand innerhalb case-Anweisung - SQL Server 2008

select * from tbl 
     where case when expr then 
     (
     ([email protected]_megacity and state_group in (select * from temp_state_megacity)) OR 
     ([email protected]_10lac and state_group in (select * from temp_state_10lac)) OR 
     ([email protected]_below10 and state_group in (select * from temp_state_below10)) OR 
     ([email protected]_rural and state_group in (select * from temp_state_rural)) 
     ) else true end 

Fehler - falsche Syntax

+0

'CASE' funktioniert nicht wie' if' in einer prozeduralen Sprache. Es gibt nur einen atomaren Wert zurück. – HoneyBadger

+0

Ok. Also, was ist die Alternative für dieses Problem? –

+0

Wählen Sie in temporäre Tabelle und verwenden Sie das dann, um spezielle Abfragen zu filtern oder zu machen –

Antwort

2

In einer WHERE Klausel Sie AND verwenden, OR und NOT Ausdrücke zu kombinieren:

select * from tbl 
    where NOT <expression> OR 
    (
    ([email protected]_megacity and state_group in (select * from temp_state_megacity)) OR 
    ([email protected]_10lac and state_group in (select * from temp_state_10lac)) OR 
    ([email protected]_below10 and state_group in (select * from temp_state_below10)) OR 
    ([email protected]_rural and state_group in (select * from temp_state_rural)) 
    );