6

我在一个过程中有多个更新和插入语句。

请参考以下示例:

程序示例

- 代码

更新 1

插入 1

更新 2

更新 3 --假设发生异常

现在我想回滚到第一个更新语句之前意味着没有更新或插入影响。

4

3 回答 3

8
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;


====== 编辑 ==========

工作示例: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;
/
于 2014-05-05T04:21:44.620 回答
0

尝试

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;

** 注意 BEGIN END 块

于 2014-05-05T07:16:58.243 回答
0

您可以在异常 when 子句中捕获异常并执行回滚语句,例如

procedure test
is
begin
    Insert into t values (1);
    Update t set x = 1;
exception when <your exception> then
    Rollback;
end;
于 2014-05-05T04:22:56.587 回答