查询返回值且没有“ NO_DATA_FOUND THEN时发生异常”(Oracle 11g)时传递数据

时间:2019-01-29 13:27:36

标签: oracle plsql oracle11g

我已经创建了一个更新我的t_ritm表的过程。首先,我从rrcd_qnty表中选择产品ID的t_rrcd(这是我的产品数量)。然后,我更新rrcd_qnty表中的t_ritm值。

这是我的程序:

procedure update_ritm_new_rate(p_oid in varchar2, p_ritm_rate in varchar2, p_euser in varchar2)
is
nrate varchar2(4);
begin
    SELECT rrcd_rate into nrate 
    FROM (select oid, t_rrcd.rrcd_rate 
           from t_rrcd 
           where rrcd_ritm= p_oid 
           ORDER BY oid DESC ) 
    WHERE rownum <= 1 
    ORDER BY rownum DESC ;  
EXCEPTION  
    WHEN NO_DATA_FOUND THEN  nrate := 0; 


    update t_ritm 
    set ritm_rate = nrate, euser = p_euser, edat = sysdate  
    where oid = p_oid;
    commit;
end update_ritm_new_rate;

我的某些产品ID数量为null。所以我遇到了No_Data_Found错误。但是何时以及哪个产品ID具有“数量”值,它们已成功更新。为了避免使用No_Data_Found,我使用了EXCEPTION WHEN NO_DATA_FOUND THEN nrate := 0;来解决我的no_Data_Found错误。但是,当产品ID具有数量值时,它们不会更新。

我在此问题上搜索了很多,但没有找到好的解决方案。避免No_Data_Found错误的最佳实践是什么?如果没有任何No_Data_Found错误,可以传递我的值吗?

预先感谢

2 个答案:

答案 0 :(得分:0)

这是因为-如果您的SELECT返回了某些内容,则它永远不会到达UPDATE,因为它被隐藏在EXCEPTION处理程序后面。

因此,请将其(SELECT放入其自己的BEGIN-END块中,然后将UPDATE放入其中,以便使用任何NRATE值执行它。

PROCEDURE update_ritm_new_rate (p_oid         IN VARCHAR2,
                                p_ritm_rate   IN VARCHAR2,
                                p_euser       IN VARCHAR2)
IS
   nrate   VARCHAR2 (4);
BEGIN
   BEGIN                                               --> this
        SELECT rrcd_rate
          INTO nrate
          FROM (  SELECT oid, t_rrcd.rrcd_rate
                    FROM t_rrcd
                   WHERE rrcd_ritm = p_oid
                ORDER BY oid DESC)
         WHERE ROWNUM <= 1
      ORDER BY ROWNUM DESC;
   EXCEPTION
      WHEN NO_DATA_FOUND
      THEN
         nrate := 0;
   END;                                               --> this

   UPDATE t_ritm
      SET ritm_rate = nrate, euser = p_euser, edat = SYSDATE
    WHERE oid = p_oid;

   COMMIT;
END update_ritm_new_rate;

答案 1 :(得分:0)

我通过在更新查询之后添加当NO_DATA_FOUND THEN nrate:= 0; 时例外。

procedure update_ritm_new_rate(p_oid in varchar2, p_ritm_rate in varchar2, p_euser in varchar2)
is
nrate varchar2(4);
begin
    SELECT rrcd_rate into nrate FROM (select oid, t_rrcd.rrcd_rate  from t_rrcd where rrcd_ritm= p_oid ORDER BY oid DESC ) 
    WHERE rownum <= 1 ORDER BY rownum DESC ;  

    update t_ritm set ritm_rate = nrate, euser = p_euser, edat = sysdate  where oid = p_oid;
    commit;

    EXCEPTION  WHEN NO_DATA_FOUND THEN  nrate := 0;
end update_ritm_new_rate;