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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18111517/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 05:12:35  来源:igfitidea点击:

Sql*plus always returns exit code 0?

oraclescriptingsqlplusexit-code

提问by Tulains Córdova

Whenever I run a sql script using Sql*plus and check for $?, I get 0 even when the script wasn't succesful.

每当我使用 Sql*plus 运行 sql 脚本并检查 $? 时,即使脚本不成功,我也会得到 0。

Example

例子

#$ 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
$

I would like it to return a non-zero value when an error occurs.

我希望它在发生错误时返回一个非零值。

How can I achieve that ?

我怎样才能做到这一点?

回答by

You have to explicitly tell sqlplusto do that, in your script. Basically, there are two statements that you can use:

您必须sqlplus在脚本中明确告知要这样做。基本上,您可以使用两种语句:

  • WHENEVER SQLERROR EXIT SQL.SQLCODE
  • WHENEVER OSERROR EXIT
  • WHENEVER SQLERROR EXIT SQL.SQLCODE
  • WHENEVER OSERROR EXIT

For example:

例如:

WHENEVER SQLERROR EXIT SQL.SQLCODE
begin
  SELECT COLUMN_DOES_NOT_EXIST FROM DUAL;
END;
/

And for OS errors:

对于操作系统错误:

WHENEVER OSERROR EXIT FAILURE
START no_such_file

For more information, see thisand that.

有关更多信息,请参阅这个那个

Hope it helps. Good Luck!

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

回答by Andrew Wolfe

Vlad's is the answer I'd use. To augment his, however, I try to use an explicit EXIT statement if I really need that return status. For example

弗拉德是我会使用的答案。但是,为了增强他的功能,如果我真的需要返回状态,我会尝试使用显式 EXIT 语句。例如

column status_for_exit new_value exitcode noprint
select status_computation (parm, parm) as status_for_exit from dual;

exit &exitcode;

回答by Karl Henselin

The best action might a combination of the other ideas on this page and the ideas at

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

Help with SQLPLUS please? How to make SQLPLUS startup with DEFINE `OFF` initially?

请帮助SQLPLUS?最初如何使用 DEFINE `OFF` 启动 SQLPLUS?

Make a login.sql file, or edit the global one to have

制作一个 login.sql 文件,或编辑全局文件以拥有

WHENEVER OSERROR EXIT FAILURE
WHENEVER SQLERROR EXIT SQL.SQLCODE

inside it. Then, if the file doesn't exist, it will error out. If a line fails, it will error out.

在里面。然后,如果该文件不存在,则会出错。如果线路失败,它将出错。

However, keep in mind that as the docs say at : https://docs.oracle.com/cd/E11882_01/server.112/e16604/ch_twelve052.htm#SQPUG135that certain commands still will not error out as you might expect.

但是,请记住,正如文档所述:https: //docs.oracle.com/cd/E11882_01/server.112/e16604/ch_twelve052.htm#SQPUG135某些命令仍然不会像您预期的那样出错。