简体   繁体   中英

PL/SQL Exception handling and update statements

I'm quite new to PL/SQL and I have a problem with displaying my exceptions that I can't seem to figure out.

I'm using two stored procedures and an anonymous block to call them. I've made a few other procedures in this manner and they all work just fine.

This is the first procedure that is updating my table.

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;  

This is the procedure that calls the above procedure. This is just displaying what I'm going to do and confirms that it worked.

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;

This is the anonymous block I'm using to call the above procedure which then calls the one above that.

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

If I pass parameters that would not give me an error, all the code works just fine.

For example, if I input;

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

I get the correct output and the changes have been made in the table.

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

However, if I pass parameters that would result in an error, either a customer id not existing or the amount being out of the range, nothing happens. I get this:

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

Nothing else.

In similar procedures my exceptions are working just fine. This example is using a procedure that inserts into a table.

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

I'm not sure why this procedure is not returning my exceptions at all. In my other procedures that work, if an exception is raised, the script output in Oracle SQL Developer just displays my exception.

However, in my update procedure, if something should raise an exception, above the dbms output lines, the script output prints this error report

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.

So it's acknowledging there that my exception has been raised, but isn't shown.

Any help is greatly appreciated, I'm thoroughly confused by this. Am I making some truly obvious mistake?

Not an expert here — just helping brainstorm. It sounds like you are getting a response but you just need to write a custom error statement. Out of range means that in the list that was selected with parameters set that it does not exist within the scope of that query. For example if list was 0-100 then response was from 0-10 and 0 was not found in that range.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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