将错误处理或异常块添加到PL / SQL中的过程

时间:2017-10-16 15:49:37

标签: sql oracle plsql exception-handling

在PL / SQL中,我需要将错误处理或异常块添加到以下insert_items过程中,该过程将错误写入nc_error表:

/* 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;
/

我该怎么做?

1 个答案:

答案 0 :(得分:0)

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;
/