oracle SQL*Plus:强制它返回错误代码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2254761/
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-18 19:59:46  来源:igfitidea点击:

SQL*Plus : Force it to return an error code

oraclesqlplusexit-code

提问by Tom

I have a stored procedure that has an OUT parameter, indicating an error code. If the error code is not 0, then I raise an error

我有一个存储过程,它有一个 OUT 参数,指示一个错误代码。如果错误代码不是 0,那么我提出一个错误

DECLARE
BEGIN
 foo (err_code);

 IF (err_code <> 0) THEN
   raise_application_error(...);

END;

So far so good, but here's my question.

到目前为止一切顺利,但这是我的问题。

This piece of code (shown above) is executed by sqlplus, which is called from a shell script, which should exit with 0 / not 0 (as the sql script).

这段代码(如上所示)由 sqlplus 执行,它是从 shell 脚本调用的,它应该以 0 / 不是 0(作为 sql 脚本)退出。

#shell script

sqlplus ... @myscript
return $?

When the raise_application_error executes, control goes back to sqlplus.

当 raise_application_error 执行时,控制权返回给 sqlplus。

sql>

What I want, is a way of exiting back to the shell, without sqlplus returning a 0 on $?

我想要的是一种退出 shell 的方法,而 sqlplus 在 $ 上不返回 0?

Any thoughts? Thanks in advance.

有什么想法吗?提前致谢。

回答by Adam Musch

If you care which application error your PL/SQL raised, you can declare the return code as a SQL*Plus variable, and return have the PL/SQL procedure set it.

如果您关心您的 PL/SQL 引发了哪个应用程序错误,您可以将返回代码声明为 SQL*Plus 变量,并返回让 PL/SQL 过程设置它。

#shell script

sqlplus /nolog << EOF
connect uid/pw
variable retval number;
BEGIN
  foo (:retval);
END;
/

exit retval;
EOF

return $?