Showing results for 
Search instead for 
Did you mean: 

Reading variable from SQL to Unix

Occasional Visitor

Reading variable from SQL to Unix

I have a korn shell script and a variable that I'm trying to load data into from a table in Oracle, this variable will then be used in a subject line in an email that will be sent from Unix. I have this so far but am having difficulty still trying to get the variable to display properly in the mail -s command. Can someone please help? Here is the code that only returns '0' in the subject line but I want to actually return the output from the SQL statement into that variable then display it in the Subject line of the Unix mail:

. /apps/ppa/bin/.profile
#export ORACLE_HOME=
export VAR=0

sqlplus /nolog << EOF
connect opcppa/oracle
set echo off;
set termout on;
set linesize 100;
set pagesize 60;
set newpage 0;
set heading off;
set feedback off;
spool /tmp/output.lis;
select COMPANY into "$VAR" from CUSTOMERS where rownum<2;
spool off;
/usr/bin/uuencode /tmp/output.lis "TestFileOracleData.txt"|mail -s "$VAR" $emailadd1


Hein van den Heuvel
Honored Contributor

Re: Reading variable from SQL to Unix

sqlplus does NOT understand shell variables.

Its only communication channel back is really STDOUT data, and for numerics in a pinch the process exit status.

Google for "sqlplus select into shell variable"
You'll get a lot of useful example.
For example you find a construct like:

COUNT=`sqlplus -s system/manager <set head off;
select count(*) from table1;

[ I'd use the preferred $( ... ) ]

For more than one number you'll need the help of the Shell READ command and perhaps toss in an AWK or PERL filter.

So please play with that thought for a while and see if you need further help!

Hein van den Heuvel.
Hein van den Heuvel
Honored Contributor
Occasional Visitor

Re: Reading variable from SQL to Unix

I will look at these and others on google.