How to use savepoints in oracle procedure

会有一股神秘感。 提交于 2019-12-05 23:15:06
BEGIN

  Savepoint do_update_1;

  Update 1;

  insert 1;

  Update 2;

  Update 3; --Suppose exception occurs

EXCEPTION
  WHEN some_exception THEN Rollback To do_update_1;
END;


====== edit ==========

Working example: http://sqlfiddle.com/#!4/b94a93/1

create table tttt(
  id int,
  val int
)
/

declare 
  x int := 0;
begin
  insert into tttt values( 1,1);
  insert into tttt values( 2,2);
  Savepoint do_update_1;

  insert into tttt values( 3,3);
  update tttt set val = 0 where id = 2;
  update tttt set val = 10 / val where id = 2;

exception
  when zero_divide then rollback to do_update_1;
end;
/

You can catch exception in exception when clause and execute rollback statement, e.g.

procedure test
is
begin
    Insert into t values (1);
    Update t set x = 1;
exception when <your exception> then
    Rollback;
end;

try

BEGIN

  BEGIN
  Savepoint do_update_1;


  Update 1;

  insert 1;

  Update 2;

  Update 3; --Suppose exception occurs

  EXCEPTION
    WHEN some_exception THEN Rollback To do_update_1;

  END;
END;

** note the BEGIN END block

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!