If you want to return a code from SQL*Plus to the UNIX shell script, then the WHENEVER SQLERROR directive can help. We can return a status from 0...255 to the UNIX shell. This is usually in the $? environment variable ( for the Korn or Bash Shell ) immediately after execution of a command. Here is a KSH script that shows how this might work:
sqlplus -s scott/tiger <<-EOF 1>/dev/null 2>&1
variable rc number
whenever sqlerror exit sql.sqlcode
-- The Variable rc is the return code (from your programm logic)
-- which you want to send from SQL*Plus to the shell, when
-- the scripts aborts due the WHENEVER SQLERROR
:rc := 1;
if (:rc <> 0)
raise_application_error((-20000-224) - :rc, 'Error!');
Run this code as follows and you will see:
$ echo $?
The 1 is what you are looking for. The key is to use ...
raise_application_error( (-20000-224) - :rc, 'Error!' );
... to raise the error. We can raise errors in a given range, but the shell will only keep an unsigned byte in the status return value (values 0...255). It takes our exit file and just looks at that last byte. By using -20000-224 and subtracting from your return code, we end up exiting with the value of your return code ( given that :rc is in the range...255 ! ).
来自 “ ITPUB博客 ” ，链接：http://blog.itpub.net/936/viewspace-60613/，如需转载，请注明出处，否则将追究法律责任。