2012-11-11 49 views
3

我正在嘗試編寫一個函數,該函數在表中已經找到帳戶描述時返回1,如果沒有,則返回0。Oracle PL-SQL函數不返回true

我可以讓它返回0很好,但我不能似乎得到它返回1

Create or replace function test_glaccounts_description 
    ( 
    var_account_desc varchar2 
    ) 
    return number 
    as var_status number 
    begin 
    select 1 into var_status 
    from general_ledger_accounts 
    where account_description = var_account_desc; 
    exception 
    when no_data_found then 
    var_status := 0; 
    return var_status; 
    end; 
    /

這是它應該做的

代碼的函數命名test_glaccounts_description接受一個參數 ,測試賬戶描述是否已經在 General_Ledger_Accounts表中。如果賬戶描述在表中,該函數應該返回值1,否則返回0。 (注:如果一個SELECT語句 不返回任何數據,它拋出一個NO_DATA_FOUND異常 你的函數可以處理。)

回答

4

您需要從所有路徑在你的代碼返回。

像這樣的東西應該工作:

begin 
    select 1 into var_status 
    from general_ledger_accounts 
    where account_description = var_account_desc; 
    return var_status; 
exception 
    when no_data_found then 
    var_status := 0; 
    return var_status; 
end; 
/
+0

啊,我不知道一個異常被認爲是一個單獨的部分感謝。 – mmarkgraf