case case语句中的条件 - SQL server 2008

时间:2016-05-06 07:51:14

标签: sql sql-server

如何在case语句中使用条件:

    select * from tbl
        where case when expr then
        (
         (x=@_megacity and state_group in (select * from temp_state_megacity)) OR
         (x=@_10lac and state_group in (select * from temp_state_10lac)) OR
         (x=@_below10 and state_group in (select * from temp_state_below10)) OR
         (x=@_rural and state_group in (select * from temp_state_rural))
        ) else true end

错误 - 语法错误

1 个答案:

答案 0 :(得分:2)

WHERE子句中,您使用ANDORNOT来组合表达式:

select * from tbl
    where NOT <expression> OR
    (
     (x=@_megacity and state_group in (select * from temp_state_megacity)) OR
     (x=@_10lac and state_group in (select * from temp_state_10lac)) OR
     (x=@_below10 and state_group in (select * from temp_state_below10)) OR
     (x=@_rural and state_group in (select * from temp_state_rural))
    );