我在程序中有多个更新和插入语句。
请参考以下示例:
程序示例
- 代码
更新1
插入1
更新2
更新3 - 发生异常
现在我想回滚到第一个更新语句之前意味着没有更新或插入影响。
答案 0 :(得分:5)
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;
/
答案 1 :(得分:0)
您可以在例外子句中捕获异常并执行回滚语句,例如
procedure test
is
begin
Insert into t values (1);
Update t set x = 1;
exception when <your exception> then
Rollback;
end;
答案 2 :(得分: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块