How do i execute oracle Query using shell Scripting and send the results in the email

sachin bhandari picture sachin bhandari · Jun 5, 2015 · Viewed 25.2k times · Source

I am new to Unix scripting.. I have an oracle DB which has a username, password, hostname, port and servicename. I want to connect the database and run the query and in the end the result should be email to me or other people. Result should be displayed in the body of an email.

Answer

Lalit Kumar B picture Lalit Kumar B · Jun 5, 2015

This is a sample shell script to connect through SQL*Plus, SPOOL the output and send it as email in the message body.

#!/bin/sh -- or bash or ksh

sqlplus -s /nolog <<EOF
CONNECT username/password@sid 
SPOOL /u01/spool.csv
--do something
SPOOL OFF
EXIT;
EOF

mail -s "Subject" [email protected] < /u01/spool.csv