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

被折叠的 条评论
为什么被折叠?



