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;
/
整件事情是否應該失敗並在第一個錯誤處回滾,還是應該繼續下一個項目?你有記錄程序嗎? –
不能在insert_item過程中包含異常,並且是否需要爲每個失敗的元素執行?如果可能,請顯示您的insert_item過程。 –
我建議你做一些閱讀而不是問這裏。此信息很容易搜索。 https://docs.oracle.com/cd/B28359_01/appdev.111/b28370/errors.htm#LNPLS007 –