[pl sql] where current of

本文探讨了在Oracle中使用SELECT FOR UPDATE时遇到的问题,包括使用显式静态游标进行更新时的ORA-06550错误及循环内提交导致的ORA-01002错误,并提供了具体的示例代码。

使用select for update 的时候的几个问题

1,使用显示静态游标

eg:

create table tb_test_1 (id number,
name varchar2(20));

insert into tb_test_1 (id, name) values (1, 'name');

declare
  my_test test.tb_test_1%rowtype;
  cursor cs is
    select * from tb_test_1 for update;
begin

  for css in cs loop
    my_test.id   := css.id;
    my_test.name := css.name || 's';
    update tb_test_1 a
       set a.id = my_test.id, a.name = my_test.name
     where current of cs;
  end loop;
  commit;

end;

 

若:

declare
  my_test test.tb_test_1%rowtype;
  type cs is ref cursor;
  css cs;
begin

  open css for
  select * from tb_test_1 for update;
 
  loop
 
  fetch css into my_test;
  exit when css%notfound;
    update tb_test_1 a
       set a.id = my_test.id, a.name = my_test.name||'s'
     where current of css;
  end loop;
  commit;

end;

则ora-06550

 

2,注意loop内不能commit

eg:

insert into tb_test_1 (id, name) values (2, 'name2');

declare
  my_test test.tb_test_1%rowtype;
  cursor cs is
    select * from tb_test_1 for update;
begin

  for css in cs loop
    my_test.id   := css.id;
    my_test.name := css.name || 's';
    update tb_test_1 a
       set a.id = my_test.id, a.name = my_test.name
     where current of cs;
     commit;
  end loop;
  commit;

end;

则:ora-01002

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

转载于:http://blog.itpub.net/16179598/viewspace-531591/

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值