I would assume that the sqlplus utility would return with non-zero return code
which you could catch either through $? or by direct shell logic.
However, I would rather rely on something like Perl's DBI (or DBD::Oracle below)
for anything in Oracle/Unix interaction.
You could continue the script with an if statement. For example, if the exception is: except, then you could insert an "if" statement in the script for
handling it:
if [[ $except -ne or -eq for not equal or for equal or Whatever option ]]
then
list of operations to perform
or
else
fi
echo "WHENEVER SQLERROR EXIT 1
set feedback off verify off pagesize 0
select column from table " | sqlplus -s/@dbname
if [[ $? -ne 0 ]] ; then
do something...
fi
Or you can do something like this:
{
echo "WHENEVER SQLERROR EXIT 1
set feedback off verify off pagesize 0
select column from table; " | sqlplus -s/@dbname
} | while IFS=$(echo '\012\001') read LINE ; do
case ${LINE} in
ORA-*|SP2-*) do some stuff based on Oracle error ;;
*) echo ${LINE} ;;
esac
done
I want to read whole lines back from Oracle queries so I am setting the field separator to newlines only for the WHILE..read loop. Otherwise, the read would read each word rather than the whole line. I use echo '\012' (\012 = newline) because I don't like to break up lines like this IFS='
'. I add \001 because if I use \012 by itself, it doesn't seem to stick for me.
line_no=10
pro=TEST
{
echo "WHENEVER SQLERROR EXIT 1
set feedback off verify off pagesize 0
select x from tp_mquote where ct1=0; " | sqlplus -s/ /
} | while IFS=$(echo '\012\001') read LINE;
do
case ${LINE} in
ORA-*|SP2-*) echo "Hido some stuff based on Oracle error" ;;
*) echo ${LINE} ;;
esac
done
echo $?
if [ $? -ne 0 ]; then
global_proc $line_no $proc_name
else
echo Success
fi
------Now their is error in sql statement as the column in WHERE clause does not exist in that table...-------------
But before echoing that error it echoes
sql statement and then all FILE NAMES in my pwd
and after that the actual error..
line_no=10
pro=TEST
set -f # <-- Turn globbing off (no filenames)
{
echo "WHENEVER SQLERROR EXIT 1
set feedback off verify off pagesize 0
select x from tp_mquote where ct1=0; " | sqlplus -s /
} | while IFS=$(echo '\012\001') read LINE;
do
case ${LINE} in
ORA-*|SP2-*)
echo "Hido some stuff based on Oracle error"
echo ${LINE} >> a.out # <-- Echo error message to a.log
;;
*) echo ${LINE} ;;
esac
done
echo $?
if [ $? -ne 0 ]; then
global_proc $line_no $proc_name
else
echo Success
fi
set +f # <-- Turn globbing back on
Many Many thanks for the help and it has worked...
I have one more problem....
Suppose my sql stmt is as follows:-
SELECT a, b, c, d, e, ,f ,g ,h FROM table;
Now I am sending the above select stmt. to a Error Handling shell script.
As Error Handling script will treat this sql statement as positional parameters ..
from $1 - $9. So this will cause problem in executing the above SQL stmt.
So what is the way to deal with it ....
If i use $* in that case it is working...
but along with sql stmt I also want to send the proc_name,Lineno variable then how should i deal with it ..
line_no=10
pro=TEST
{
echo "WHENEVER SQLERROR EXIT 1
set feedback off verify off pagesize 0
select x from tp_mquote where ct1=0; " | sqlplus -s/ /
} | while IFS=$(echo '\012\001') read LINE;
do
case ${LINE} in
ORA-*|SP2-*) echo "Hido some stuff based on Oracle error" ;;
*) echo ${LINE} ;;
esac
The echo "WHENEVER SQLERROR EXIT 1 ---- THis line ouputs the error
to the screen....I dont want it ...
Instead I am writing it in a file ...