Skip to Main Content
  • Questions
  • Calling SQL file from batch file with return vairables

Breadcrumb

XMAS_TREE

The AskTOM team is taking a break over the holiday season, so we're not taking questions or responding to comments. Please have a happy and safe festive season and as always, thanks for being a member of the AskTOM community.

Question and Answer

Connor McDonald

Thanks for the question.

Asked: February 23, 2017 - 8:12 pm UTC

Last updated: February 24, 2017 - 7:57 am UTC

Version: 11g

Viewed 1000+ times

You Asked

Hi..

I have to write a batch file which will call a sql file by passing one parameter. The sql file has to select two columns from a table and return them to the batch file and the batch file should display it on the screen.

Can I get some inputs or help in writing this.

Many thanks

and Connor said...

I like funnelling all of my output to a file, so I can check the file for errors as well as extracting the content I want. So I might have something like this (I'm using ksh in this instance)

tmpfile=/tmp/myjob.$$

print "
connect / as sysdba
set pages 0
set lines 200
set feedback off
set trimspool on
set serverout on
select '@COUNT '||count(*) x from all_tables
exit" | sqlplus -s /nolog 1>$tmpfile 2>&1

errs=`egrep '(SP2|ORA-)' $tmpfile`
if [ "$errs" ] ; then
  cat $tmpfile | mailx -s "ERROR: SOMETHING FAILED $bkptime" ops@business.com
  exit
fi
obj_count=`egrep '^@COUNT' $tmpfile | awk '{print $2}'`




We're not taking comments currently, so please try again later if you want to add a comment.