加入收藏 | 设为首页 | 会员中心 | 我要投稿 辽源站长网 (https://www.0437zz.com/)- 云专线、云连接、智能数据、边缘计算、数据安全!
当前位置: 首页 > 站长百科 > 正文

oracle11g – Oracle 11 – sqlplus – 回滚整个脚本的错误 –

发布时间:2021-01-16 12:33:42 所属栏目:站长百科 来源:网络整理
导读:如何在包含的SQL文件中的任何错误上使Oracle 11g回滚整个事务? 文件内容是: set autocommit offwhenever SQLERROR EXIT ROLLBACKinsert into a values (1);insert into a values (2);drop index PK_NOT_EXIST;commit; 并使用“@”将文件包含在sqlplus会话

如何在包含的SQL文件中的任何错误上使Oracle 11g回滚整个事务?

文件内容是:

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条记录,这意味着在退出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;
/

(编辑:辽源站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读