如何使 Oracle 11g 在包含的 SQL 文件中出现任何错误时回滚整个事务?
文件内容是:
set autocommit off
whenever SQLERROR EXIT ROLLBACK
insert into a values (1);
insert into a values (2);
drop index PK_NOT_EXIST;
commit;
并且使用“@”将该文件包含到 sqlplus 会话中:
@error.sql
正如预期的那样,sqlplus 会话终止并且输出为
SQL> @error.sql
1 row created.
1 row created.
drop index PK_NOT_EXIST *
ERROR at line 1:
ORA-01418: specified index does not exist
Disconnected from Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
但是当我重新启动sqlplus时,表a包含2条记录,这意味着有一个commit而不是 sqlplus 退出时的回滚。
我可以以某种方式强制 sqlplus :
- 出错时停止处理文件,
- 并在错误时回滚整个事务?
DDL 在运行之前和之后都会执行提交,这样即使 DDL 失败,oracle 也已经提交了事务。
你可以用以下方法解决它:
set autocommit off
whenever SQLERROR EXIT ROLLBACK
declare
procedure drop_idx(i varchar2)
is
pragma autonomous_transaction; -- this runs in its own transaction.
begin
execute immediate 'drop index ' || i;
end;
begin
insert into a values (1);
insert into a values (2);
drop_idx('PK_NOT_EXIST');
end;
/
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)