2014-09-01 46 views
0

我有一個用戶放入一年的文本字段,即:the_SCHEME_YEAR。用戶然後獲得該年的數據。但是,當用戶離開文本字段空白時,我想把所有年份都拉回來。當WHERE中的綁定變量爲NULL時,返回所有數據

這是我開發的代碼,但是當客戶留下空白時,無法獲得所有年份。

AND the_year in NVL(:the_SCHEME_YEAR, to_char('2005,'||'2006,'||'2007')) 

任何提示將有所幫助。

SELECT the_year 
    , ent_type 
    , SUM (a.no_of_parts) number_ents 
    , SUM (ent_value * a.no_of_ents) value_ents 
    FROM TEST_REGISTER A 
WHERE the_year > 2004 and the_year <= the_max_year 
    AND ent_type is not null 
    AND the_year in NVL(:the_SCHEME_YEAR, to_char('2005,'||'2006,'||'2007')) 
GROUP BY the_year, ent_type 
ORDER BY 1 

回答

0

試試這個:

AND ( (:the_SCHEME_YEAR is null and the_year in (2005, 2006, 2007)) 
    OR the_year = :the_SCHEME_YEAR 
    ) 

(注意SQL in運營商需要值的列表,還沒有一個連接字符串。)

+0

謝幕了託尼。我一直在使用單選按鈕和LOV,並且知道必須有一些SQL代碼才能完成這項工作。非常感謝你。 – 2014-09-01 11:23:52