简体   繁体   中英

unix script to exit from oracle error when using a wait statement

I have a ksh shell script that runs a .sql script with a background and wait statement. Is it possible for me to capture the generic "ORA-0" error and exit out completely?

So far:

$ORACLE_BASE/bin/sqlplus 2.sql &
pid2=$!
echo "Waiting for PID:$pid2"
wait $pid2
#look for error here
#exit program if oracle error

SQL*Plus is kind of a shell itself, so it catches the errors from oracle and continues execution. You want to look at the SQL*Plus command WHENEVER SQLERROR , especially WHENEVER SQLERROR EXIT that allows to exit immediately with an error code.

So, at the beginning of your .sql script you add something like :

WHENEVER SQLERROR EXIT SQL.SQLCODE

And if there is an SQL error it will exit with the relevant error code.

Now, in the shell script after the wait you can get the error by reading $? .

Something like (depending on the exact syntax of your shell) :

wait $pid2
ret=$?
if [ $ret != 0 ]  # if not success
then
    exit $ret     # propagate error code
fi

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM