I wish to write a shell script that calls a sql procedure(ultimately run the script through a scheduler) and if the procedure gives an sql error of any kind, send the error as an email which i wish to specify inside the script.
How can I do it?
EDIT: Here is what I tried. I cannot figure out how to put the logic that send email only if there is error.
export ORACLE_HOME=/oracle/app/products/11gr1/db
export ORACLE_SID=CTPP01S1
MAIL_TO="xxx@yyy.com"
LOGFILE=./xxx_job.log
exec 2>&1 > $LOGFILE
echo " Job run at: `date`......"
$ORACLE_HOME/bin/sqlplus -s '/ as sysdba' <<EOF
set head on;
set feed on;
set serveroutput on;
set linesize 250;
set pagesize 1000;
exec schema.proc_name;
exit
EOF
echo "Job ended at: `date`"
mailx -s "OLBB Extract Results." $MAIL_TO < $LOGFILE
If you want to send the mail from Oracle then utl_mail is your friend. As @Tony noted you didn’t specify your platform, but in Linux there’s mail and in Unix both of which you can pass the output of your execution to.
In any environment you can execute your SQL in a wrapper such as Python (I’m biased) and mail from that.