How to write a shell script to run a sql query and send the query results in table format in email?
0
votes
2
answers
11031
views
I need to run a SQL query using SHELL script in PUTTY and send the results in email body in table format with headers along with a message line on top.
I have written this so far but I am not able to get results in table format.
There are no errors. But no headers in CSVFILE even after putting SET HEAD ON. I have also tried the 'mail -a (content type)' solution but '-a' gives error.
#! /bin/ksh
sqlplus -s user/pass @sid > /dev/null << EOF
whenever sqlerror exit 4;
set newpage 0
set space 0
set feedback off
set head off
set echo off
set pagesize 0
set serverout on
set trimspool on
set linesize 3000
spool $CSVFILE
select * from table;
spool off;
exit 0;
EOF
mailx -s "Subject Line" myname@email.com < $CSVFILE
exit 0
The SQL is running fine and data is being saved in CSVFILE. The result I want should be in table format sent to the email address in email body and also involve a message line above table, something like so:

Asked by UserGoogle
(1 rep)
Dec 2, 2022, 11:52 PM
Last activity: Dec 4, 2022, 05:07 AM
Last activity: Dec 4, 2022, 05:07 AM