首页 文章

Sql * plus总是返回退出代码0?

提问于
浏览
17

每当我使用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
$

我希望它在发生错误时返回非零值 .

How can I achieve that ?

3 回答

  • 2

    您必须在脚本中明确告诉 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
    

    有关更多信息,请参阅thisthat .

    希望能帮助到你 . 祝好运!

  • 22

    弗拉德是我使用的答案 . 但是,为了扩充他,我尝试使用显式EXIT语句,如果我真的需要返回状态 . 例如

    column status_for_exit new_value exitcode noprint
    select status_computation (parm, parm) as status_for_exit from dual;
    
    exit &exitcode;
    
  • 6

    最好的行动可能是本页面上其他想法和想法的结合

    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所说,某些命令仍然不会像您预期的那样错误 .

相关问题