I am trying to run a shell script (ksh) that runs oracle sqlplus to run a sql script in the background. Within the sql script I am using WHENEVER SQLERROR EXIT SQL.SQLCODE to trap the error code so I can check it and exit my program, else continue to next sql statement.
I'm not sure if my problem is in my shell script trapping the error or the use of WHENEVER?
I am producing/invoking the ORA- error, but return code still shows as Return_code=0, (pass) and continues to run the next script. It should fail and exit the program.
Can someone help me configure this script properly? My if-then-else logic maybe flawed too. Thanks.
Here's my sql (whenever.sql) script to invoke error:
WHENEVER SQLERROR EXIT SQL.SQLCODE
begin
SELECT COLUMN_DOES_NOT_EXIST FROM DUAL;
END;
/
Here's my script:
KEY=$BASEDIR/.keyinfo;
LOG=$BASEDIR/run_tst.log;
# Check before we run
if [ -f "$KEY" ]
then
IFS="
"
set -A arr $(cat $KEY)
echo "Running Test ===>$TIMESTAMP" >> $LOG 2>&1
/bin/sqlplus ${arr[0]}/${arr[1]} @whenever.sql &
pid1=$!
echo "Waiting for PID:$pid1" >> $LOG 2>&1
wait $pid1
ret=$?
echo "Return_code=$?" >> $LOG 2>&1
if [ $ret !=0 ] #if not success
then
exit $ret
echo "Error found...Return_code=$?" >> $LOG 2>&1
echo "Error found...exiting program ===>$TIMESTAMP" >> $LOG 2>&1
exit 1
else
/bin/sqlplus ${arr[0]}/${arr[1]} @tst2.sql
fi
else
echo "key not found. Exiting. ==>$TIMESTAMP" $LOG 2>&1
fi
exit 0
Results (Showing 0, since there is error, should be something else other than 0).
Running Test ===>20130825-09:25
Waiting for PID:6383
Return_code=0
I also tried WHENENVER SQLERROR EXIT 1 and still getting same result of Return_code=0
Output from testing:
Oracle Database 11g Enterprise Edition Release 11.1.0.7.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
SELECT COLUMN_DOES_NOT_EXIST FROM DUAL;
*
ERROR at line 2:
ORA-06550: line 2, column 10:
PL/SQL: ORA-00904: "COLUMN_DOES_NOT_EXIST": invalid identifier
ORA-06550: line 2, column 3:
PL/SQL: SQL Statement ignored
Disconnected from Oracle Database 11g Enterprise Edition Release 11.1.0.7.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
./run.sh[38]: test: Specify a parameter with this command.
sqlplusalways returns 0 (success) even when the SQL it executes fails. I've known other SQL command interpreters work like that — and it makes them painful to use in shell scripts. Maybe you should run the broken SQL directly from yourkshcommand line and look at$?immediately aftersqlplusexits. If it says 0, that is the problem. If not, let me know and I'll remove this comment.&, wouldn't I want to look for$!instead?&), your only interest in$!(the background process's PID) is perhaps to wait for it to finish. I don't think you can find the exit status of a background process in shell — though there might be abashextension I've not noticed (discovered, searched for) to get it that I'm not aware of. When you run something in background, you're saying "get on with; I don't care about the result", roughly. In your script, you barely need to use background. You run the process, report its PID, and then wait for it.wait $!because of the C shell's baleful influence onbash. But, you can capture$!in a variable:x=$!and thenwait $x, and the exit status$?of thewaitis the exit status of the background process.echo "Return_code=$?" >> $LOG 2>&1 if [ $ret !=0 ] #if not success then exit $ret echo "Error found...Return_code=$?" >> $LOG 2>&1 echo "Error found...exiting program ===>$TIMESTAMP" >> $LOG 2>&1 exit 1has numerous problems. The first and second echos should use$retinstead of$?; the test condition is malformed (if [ $ret != 0 ]); theexit $retmeans the following echos are not executed.