批量绑定中的SAVE EXCEPTION 子句

Oracle 从9i 开始增强了批 DML 在发生异常时处理的错误及失败的程序执行能力。这是通过
FORALL 语句的 SAVE EXCEPTION 子句来实现,其语法如下:

FORALL index in lower..upper save exceptions

SAVE EXCEPTIONS 子句是 oracle 9i 中引入的,它在一个隐式游标属性
SQL%BULK_EXCEPTIONS 中保存错误行,允许 FORALL 语句继续处理其余行。

下面这一段代码是 SQL%BULK_EXCEPTIONS 的应用例子:

[@more@]

create or replace procedure batch_dml(errnum outnumber,errtext outvarchar2)
is


type user_id_tab istableofnumberindexbybinary_integer;
type user_name_tab istableofvarchar2( 20 ) indexbybinary_integer;
type user_sex_tab istableofvarchar2( 2 ) indexbybinary_integer;

user_id user_id_tab;
user_name user_name_tab;
user_sex user_sex_tab;

bulk_bind_excep EXCEPTION;
pragmaexception_init(bulk_bind_excep,- 24381 );
begin
for idx in 1 .. 50000 loop --3?ê??ˉindex-by????
user_id(idx):=idx;
user_name(idx):= 'xxx' ||idx;
user_sex(idx):= 'F' ;
endloop;
user_id( 40000 ):= 39999 ;
user_id( 10000 ):= 9999 ;
deletefrom t_user;
forall idx in user_id.first..user_id.lastsaveexceptions
insertinto t_user values(user_id(idx), user_name(idx),user_sex(idx));
errnum:= 0 ;
errtext:= '' ;
exception
when bulk_bind_excep then
for i in 1 ..sql%bulk_exceptions.countloop
dbms_output.put_line( 'Iteration '
||SQL%bulk_exceptions(i).error_index|| 'failed with error '
||sqlerrm(sql%bulk_exceptions(i).error_code));
endloop;
commit;
whenothersthen
commit;
errnum:=sqlcode;
errtext:=sqlerrm;
end batch_dml;


这个例子是修改了上面的程序,加上 save exceptions 异常处理,当批 dml
发生异常时也能正常运行。

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/3898/viewspace-1006842/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/3898/viewspace-1006842/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值