[pl sql] where current of

yellowlee發表於2009-01-07

使用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/,如需轉載,請註明出處,否則將追究法律責任。