抑制ORA-01403:没有找到数据异常

时间:2011-08-30 07:28:22

标签: oracle plsql

我有以下代码

SELECT SUM(nvl(book_value,
                   0))
    INTO v_balance
    FROM account_details
    WHERE currency = 'UGX';

--Write the balance away

SELECT SUM(nvl(book_value,
                   0))
    INTO v_balance
    FROM account_details
    WHERE currency = 'USD';

--Write the balance away

现在问题是,表格中可能没有针对该特定货币的数据,但可能存在“美元”货币的数据。所以基本上我想在我的变量中选择总和,如果没有数据,我希望我的存储过程继续,而不是抛出01403异常。

我也不想将每个select语句都放在BEGIN EXCEPTION END块中,所以有一些方法可以抑制异常,只需将v_balance变量保留在未定义(NULL)状态而不需要异常块?

2 个答案:

答案 0 :(得分:2)

select nvl(balance,0) 
into v_balance
from 
(
    select sum(nvl(book_value,0)) as balance
    from account_details
    where currency = 'UGX'
);

答案 1 :(得分:0)

SELECT L1.PKCODE L1CD,  L1.NAME L1N, L1.LVL L1LVL,
          L2.PKCODE L2CD, L2.NAME L2N,  L2.LVL L2LVL,
          L5.PKCODE L5CD, L5.NAME L5N,

INFOTBLM.OPBAL (  L5.PKCODE, :PSTDT, :PSTUC, :PENUC, :PSTVT, :PENVT ) OPBAL,
INFOTBLM.DEBIT    ( L5.PKCODE, :PSTDT,:PENDT, :PSTUC, :PENUC, :PSTVT, :PENVT ) AMNTDR,
INFOTBLM.CREDIT ( L5.PKCODE, :PSTDT,:PENDT, :PSTUC, :PENUC, :PSTVT, :PENVT ) AMNTCR

FROM   FSLVL  L1, FSLVL L2,  FSMAST L5

WHERE  L2.FKCODE  =  L1.PKCODE
AND        L5.FKCODE  =  L2.PKCODE

AND        L5.PKCODE Between :PSTCD AND NVL(:PENCD,:PSTCD)

GROUP BY  L1.PKCODE ,  L1.NAME , L1.LVL ,
                 L2.PKCODE , L2.NAME ,  L2.LVL ,
                 L5.PKCODE , L5.NAME 

ORDER BY L1.PKCODE, L2.PKCODE,  L5.PKCODE