2014-01-22 74 views
0

我想在Oracle數據庫中執行查詢。該查詢在where子句中有案例構造。Oracle - 案例在哪裏條款

where 
    sale.op = 2 and 
     case when (:stat = -11) then (sale.type_id = 27 or sale.type_id = 28 or sale.type_id = 29) 
    else 
    (sale.type_id = 27) 
    end 

但我收到以下錯誤:

ORA-00907: missing right parenthesis.

在德比SQL這個工程。有人可以幫助我嗎? 謝謝。

+0

CASE評估條件並返回一個「表達式」,需要使用運算符評估某個值/列。 – Incognito

回答

5
where sale.op = 2 
and ( (:stat = -11 and sale.type_id in (27, 28, 29)) 
     or (:stat <> -11 and sale.type_id = 27) 
    ) 
1

嘗試此查詢:

where 
    sale.op = 2 and 
     ((:stat = -11 and (sale.type_id = 27 or sale.type_id = 28 or sale.type_id = 29)) 
     or (:stat <> -11 and sale.type_id = 27)) 
1

你也可以和嘗試沒有情況下,使用簡單的操作符和OR:

where 
    sale.op = 2 and ((:stat = -11 and (sale.type_id = 27 or sale.type_id = 28 or sale.type_id = 29)) 
     OR (:stat <> -11 and sale.type_id = 27)) 
1

嘗試此查詢

where 
    sale.op =2 and 
    ((:stat = -11 and sale.type_id=any(27,28,29)) or 
     (:stat <> -11 and sale.type_id = 27)) 

它看起來更清晰!