WHENEVER SQLERROR never works

旧时模样 提交于 2019-12-24 00:26:25

问题


I don't know what can be a reason and I spent already hours on the Web trying to figure out what's wrong in my case. I've checked official documentation and some thought from Tom Kyte on this and surely topic on stackoverflow about it.

I use following code:

.. < <(sqlplus -s /nolog <<EOF
         set errorlogging on
         WHENEVER OSERROR EXIT FAILURE
         WHENEVER SQLERROR EXIT FAILURE
         connect ${use}/${pwd}@${database}
         @$SCRIPTS_PATH/script.sql
         EOF
          )

echo Exited with $?

I've emulated different failure scenarios such as :

  • used a wrong user name
  • made syntax error in the sql squery

But this guy all the time returns the same result:

Exited with 0

I've tried different ways, such as

WHENEVER SQLERROR EXIT SQL.SQLCODE
WHENEVER SQLERROR EXIT FAILURE 

and so on but none of them worked.

Most of the people on internet say that this works for them. So I'm getting confused here...

Probably, this feature somehow related with the version of my SQL*Plus / Oracle / shell / Unix. But I didn't encounter information about such restrictions.

What am I missing here?

If anybody can share any ideas I'll be very grateful. Thank you in advance.


回答1:


Consider below generic snippet:

$ cmd1 < <(cmd2) # Or cmd1 <(cmd2)
$ echo $?

Here, $? is set to the exit status of cmd1. The exit status of cmd2 is lost.

In your case, sqlplus happens to be cmd2. So, the exit status of that command is not captured in $?.

You could try this;

$ sqlplus ... | cmd1
$ status=(${PIPESTATUS[@]})
$ for i in ${status[@]}; do
>     [ $i -ne 0 ] && echo Exited with $i
> done
$ echo Exited with 0

Note that if cmd1 is a complex structure (like while read e.g.) any thing you run in that while loop will be run in a subshell & any environment (variables/ pwd) changed will be lost.



来源:https://stackoverflow.com/questions/37824023/whenever-sqlerror-never-works

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!