每当我使用Sql * plus运行sql脚本并检查$?时,即使脚本不成功,我也会得到0。
实施例
#$ sqlplus user/password@instance @script.sql
SQL*Plus: Release 10.2.0.1.0 - Production on Wed Aug 7 14:20:44 2013
Copyright (c) 1982, 2005, Oracle. All rights reserved.
Connected to:
Oracle9i Enterprise Edition Release 9.2.0.8.0 - 64bit Production
With the Partitioning, OLAP and Oracle Data Mining options
JServer Release 9.2.0.8.0 - Production
v$dataf-ile d,
*
ERROR at line 6:
ORA-00933: SQL command not properly ended
Disconnected from Oracle9i Enterprise Edition Release 9.2.0.8.0 - 64bit Production
With the Partitioning, OLAP and Oracle Data Mining options
JServer Release 9.2.0.8.0 - Production
$ echo $?
0
$
我希望在发生错误时返回非零值。
我该如何实现?
答案 0 :(得分:23)
您必须在脚本中明确告诉sqlplus
这样做。基本上,您可以使用两种语句:
WHENEVER SQLERROR EXIT SQL.SQLCODE
WHENEVER OSERROR EXIT
例如:
WHENEVER SQLERROR EXIT SQL.SQLCODE
begin
SELECT COLUMN_DOES_NOT_EXIST FROM DUAL;
END;
/
对于操作系统错误:
WHENEVER OSERROR EXIT FAILURE
START no_such_file
希望它有所帮助。祝你好运!
答案 1 :(得分:6)
column status_for_exit new_value exitcode noprint
select status_computation (parm, parm) as status_for_exit from dual;
exit &exitcode;
答案 2 :(得分:2)
最佳操作可能是本页面上其他想法与
中的想法相结合Help with SQLPLUS please? How to make SQLPLUS startup with DEFINE `OFF` initially?
创建一个login.sql文件,或编辑全局文件
WHENEVER OSERROR EXIT FAILURE
WHENEVER SQLERROR EXIT SQL.SQLCODE
在里面。
然后,如果文件不存在,则会出错。如果一行失败,则会出错。
但是,请记住,正如文档所述:https://docs.oracle.com/cd/E11882_01/server.112/e16604/ch_twelve052.htm#SQPUG135 某些命令仍然不会像你期望的那样出错。
答案 3 :(得分:0)