【问题标题】:Add error handling or an exception block to a procedure in PL/SQL向 PL/SQL 中的过程添加错误处理或异常块
【发布时间】:2018-03-28 04:56:41
【问题描述】:

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

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

我该怎么做?

【问题讨论】:

  • 应该整个事情失败并在第一个错误处回滚,还是应该继续下一个项目?你有记录程序吗?
  • 您不能在您的 insert_item 过程中包含异常吗?是否需要为每个失败的元素执行此操作?如果可能,显示您的 insert_item 过程。
  • 我建议你阅读一下这个而不是在这里问。此信息很容易搜索。 docs.oracle.com/cd/B28359_01/appdev.111/b28370/…

标签: sql oracle plsql exception-handling


【解决方案1】:
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;
/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-10
    • 1970-01-01
    • 2016-01-26
    • 1970-01-01
    相关资源
    最近更新 更多