2016-05-06 9 views
1
case文の内部で条件を使用する方法

条件 - 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 

エラー - 不正な構文

あなたが式を結合する ANDORNOTを使用 WHERE句で
+0

'CASE'は手続き型言語で' if'のように動作しません。これは単なる原子値を返します。 – HoneyBadger

+0

では、この問題の代替手段は何ですか? –

+0

一時テーブルに選択し、それを使用してフィルタリングまたは特殊なクエリを作成します –

答えて

2

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)) 
    );