在 sqlplus 脚本中出现错误时停止

Jan*_*Jan 3 oracle sqlplus plsql oracle-sql-developer

我正在使用多个 .sql 调用的 sql 文件部署 pl/sql 代码@@file。如果包出现编译错误,脚本会继续执行到最后。

有没有办法停止每个编译错误?

我试过了,WHENEVER SQLERROR EXIT SQL.SQLCODE但脚本仍在继续。

Phi*_*lᵀᴹ 5

如果没有某种解决方法,这是无法完成的,所以这里有一个适合您。

您可以在创建后重新编译 PL/SQL 并在重新编译失败时引发异常。这将导致 SQL*Plus 在失败时退出。

例如:

测试.sql:

create or replace procedure foo
as
begin
  this is an error;
end;
/

exec execute immediate 'alter procedure foo compile'; exception when others then raise_application_error(-20000,'compilation error')
Run Code Online (Sandbox Code Playgroud)

例子:

[oracle@node1 ~]$ sqlplus phil/phil

SQL*Plus: Release 11.2.0.2.0 Production on Fri Dec 14 20:45:47 2012

Copyright (c) 1982, 2010, Oracle.  All rights reserved.


Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.2.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options

SQL> WHENEVER SQLERROR EXIT SQL.SQLCODE
SQL> @test.sql

Warning: Procedure created with compilation errors.

BEGIN execute immediate 'alter procedure foo compile'; exception when others then raise_application_error(-20000,'compilation error'); END;

*
ERROR at line 1:
ORA-20000: compilation error
ORA-06512: at line 1


Disconnected from Oracle Database 11g Enterprise Edition Release 11.2.0.2.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
[oracle@node1 ~]$
Run Code Online (Sandbox Code Playgroud)