2017-10-16 8 views
-2

En PL/SQL, je dois ajouter la gestion des erreurs ou un bloc d'exception à la procédure de insert_items suivante qui écrit des erreurs à la table nc_error:gestion des erreurs Ajouter ou un bloc d'exception à une procédure PL/SQL

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

Comment est-ce que je fais ceci?

+2

Est-ce que tout devrait échouer et revenir à la première erreur, ou devrait-on passer à l'élément suivant? Avez-vous une procédure d'enregistrement? –

+0

ne pouvez-vous pas inclure l'exception dans votre procédure insert_item et doit-elle être effectuée pour chaque élément qui a échoué? Montrez votre proc insert_item si possible. –

+0

Je vous suggère de faire quelques lectures à ce sujet au lieu de demander ici. Cette information est facilement consultable. https://docs.oracle.com/cd/B28359_01/appdev.111/b28370/errors.htm#LNPLS007 –

Répondre

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