繁体   English   中英

PL / SQL异常处理和更新语句

[英]PL/SQL Exception handling and update statements

我是PL / SQL的新手,在显示我似乎无法弄清楚的异常时遇到了问题。

我正在使用两个存储过程和一个匿名块来调用它们。 我以这种方式做了一些其他的程序,它们都工作正常。

这是更新我的表的第一个过程。

create or replace procedure UPD_CUST_SALESYTD_IN_DB (pcustid number, pamt number) AS
err_pamt exception;
err_pcustid exception;
vcount number;
begin
select count(*) into vcount from customer where
custid = pcustid;

if vcount = 0 then raise err_pcustid;
end if;

if pamt <-999.99 or pamt >999.99 then raise err_pamt;
end if;

update customer set sales_ytd = sales_ytd + pamt 
where custid = pcustid;

exception
when err_pcustid then
  RAISE_APPLICATION_ERROR(-20031, 'Customer ID not found');
when err_pamt then
  RAISE_APPLICATION_ERROR(-20032, 'Amount out of range');
when others then
  RAISE_APPLICATION_ERROR(-20000, SQLERRM);
end;  

这是调用上述过程的过程。 这只是显示我将要执行的操作并确认它有效。

create or replace procedure UPD_CUST_SALESYTD_VIASQLDEV (pcustid number, pamt number) AS
begin
dbms_output.put_line('--------------------------------------------');
dbms_output.put_line('Updating SalesYTD. Customer Id: ' || pcustid || ' Amount: ' || pamt);
UPD_CUST_SALESYTD_IN_DB(pcustid, pamt);
commit;
dbms_output.put_line('Udpate OK');
exception
when others then
  RAISE_APPLICATION_ERROR(-20000, SQLERRM);
end;

这是我用来调用上述过程的匿名块,然后再调用上面的过程。

set serveroutput on;
begin
UPD_CUST_SALESYTD_VIASQLDEV(3,999.9);
end;

如果我传递不会给我错误的参数,则所有代码都可以正常工作。

例如,如果我输入;

set serveroutput on;
begin
upd_cust_salesytd_viasqldev(3,400);
end;

我得到正确的输出,并且已在表中进行了更改。

--------------------------------------------
Updating SalesYTD. Customer Id: 3 Amount: 400
Udpate OK

但是,如果我传递会导致错误的参数,或者客户ID不存在或金额超出范围,则什么都不会发生。 我得到这个:

--------------------------------------------
Updating SalesYTD. Customer Id: 3 Amount: 1000

没有其他的。

在类似的过程中,我的例外情况很好。 本示例使用一个插入表的过程。

--------------------------------------------
Adding Customer. ID: 500  Name: Helen Nolan
ORA-20002: Customer ID out of range

我不确定为什么此过程根本不返回我的异常。 在其他有效的过程中,如果引发异常,则Oracle SQL Developer中的脚本输出仅显示我的异常。

但是,在我的更新过程中,如果在dbms输出行上方出现某些异常,则脚本输出将显示此错误报告

Error report -
ORA-20000: ORA-20032: Amount out of range
ORA-06512: at "S4931645.UPD_CUST_SALESYTD_VIASQLDEV", line 10
ORA-06512: at line 2
20000. 00000 -  "%s"
*Cause:    The stored procedure 'raise_application_error'
           was called which causes this error to be generated.
*Action:   Correct the problem as described in the error message or contact
           the application administrator or DBA for more information.

因此,它在这里确认我的异常已提出,但未显示。

非常感谢您的帮助,对此我感到很困惑。 我在犯一些真正明显的错误吗?

这里不是专家-只是帮助头脑风暴。 听起来您正在获得响应,但是您只需要编写一个自定义错误语句即可。 超出范围表示在使用参数集选择的列表中,该列表在该查询范围内不存在。 例如,如果列表为0-100,则响应为0-10,而在该范围内未找到0。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM