2012-11-20 79 views
1

我需要一個過濾條件包含在一個選擇查詢當一個條件包裝在一個函數滿足。 如果條件不滿足,則不應應用過濾條件。CASE構造在哪裏條款

代碼:

select col_a,col_b,cancel_Date from tab_a 
where col_c = <<some_condition>> 
and (case when oracle_function() = 'YES' then 'tab_a.cancel_date is null' 
     else null 
    ) 

如果我這樣寫,那麼就拋出了「無效的關係運算符」的錯誤。 意識到情況下應解析到右邊的值,我嘗試添加像這樣不給我預期的結果,當條件滿足:

select col_a,col_b,cancel_Date from tab_a 
where col_c = <<some_condition>> 
and (case when oracle_function() = 'YES' then 'tab_a.cancel_date is null' 
     else '1' 
    ) = '1' 

我預期的輸出應該是:

如果oracle_function()= YES,那麼我的查詢應該有效地解析爲:

select col_a,col_b,cancel_Date from tab_a 
where col_c = <<some_condition>> and tab_a.cancel_date is null; 

否則:

select col_a,col_b,cancel_Date from tab_a 
where col_c = <<some_condition>>; 

有什麼想法?

回答

2

取而代之的是case語句,這個查詢要求要麼函數返回以外的值比'YES',或該日期爲空......這應該是類似於原始查詢:

select col_a,col_b,cancel_Date 
from tab_a 
where col_c = <<some_condition>> 
and (oracle_function() <> 'YES' OR tab_a.cancel_date is null) 
+0

哼。 ..只是想着它!謝謝 ! – Casey