简体   繁体   中英

Add error handling or an exception block to a procedure in PL/SQL

In PL/SQL, I need to add error handling or an exception block to the following insert_items procedure that writes errors to the nc_error table:

/* Create draft insert_items procedure. */
CREATE PROCEDURE insert_items
( pv_items ITEM_TAB ) IS

BEGIN
/* Read the list of items and call the insert_item procedure. */
FOR i IN 1..pv_items.COUNT LOOP
insert_item( pv_item_barcode => pv_items(i).item_barcode
, pv_item_type => pv_items(i).item_type
, pv_item_title => pv_items(i).item_title
, pv_item_subtitle => pv_items(i).item_subtitle
, pv_item_rating => pv_items(i).item_rating
, pv_item_rating_agency => pv_items(i).item_rating_agency
, pv_item_release_date => pv_items(i).item_release_date );
END LOOP;
END;
/

How do I do this?

1. You could have something like this if you want exception to the whole procedure insert_item as exception occurs the process will terminate inserting error details into the nc_error table:
CREATE PROCEDURE insert_items
( pv_items ITEM_TAB ) IS
v_errcode number;
v_err_msg varchar2(4000);
BEGIN
/* Read the list of items and call the insert_item procedure. */
FOR i IN 1..pv_items.COUNT LOOP
insert_item( pv_item_barcode => pv_items(i).item_barcode
, pv_item_type => pv_items(i).item_type
, pv_item_title => pv_items(i).item_title
, pv_item_subtitle => pv_items(i).item_subtitle
, pv_item_rating => pv_items(i).item_rating
, pv_item_rating_agency => pv_items(i).item_rating_agency
, pv_item_release_date => pv_items(i).item_release_date );
END LOOP;
commit;
exception 
when others then
v_errcode := sqlcode;
v_err_msg := substr(sqlerrm,1,4000);
insert into nc_error(sql_code,sql_error_msg) select v_errcode ,v_err_msg from dual;
commit;
END;
/

2. Alternative, iF you want exception to be catched for each record insert in the for loop then exception handling needs to be done in the insert_item procedure
add an out parameter called output_status number,error_msg and error_code
and use begin exception block in the insert_item procedure and for successful completion of insert commit the record and 
assign output_status as value 0,error_msg as null and error_code as null and 
in the exception block assign this out paramter output_status as 1.error_msg as sqlerrm and error_code as sqlcode.I could have shown you as example but code for insert_item procedure is not there .

now after this,
CREATE PROCEDURE insert_items
( pv_items ITEM_TAB ) IS
v_output_status number;
v_errcode number;
v_err_msg varchar2(4000);
BEGIN
/* Read the list of items and call the insert_item procedure. */
FOR i IN 1..pv_items.COUNT LOOP
insert_item( pv_item_barcode => pv_items(i).item_barcode
, pv_item_type => pv_items(i).item_type
, pv_item_title => pv_items(i).item_title
, pv_item_subtitle => pv_items(i).item_subtitle
, pv_item_rating => pv_items(i).item_rating
, pv_item_rating_agency => pv_items(i).item_rating_agency
, pv_item_release_date => pv_items(i).item_release_date,
output_status =>  v_output_status,
error_msg =>  v_err_msg,
error_code => v_errcode);

if v_output_status = 1 then 
insert into nc_error(barcode,sql_code,sql_error_msg) select pv_items(i).item_barcode,v_errcode ,v_err_msg from dual;
end if;
commit;
END LOOP;
END;
/

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