Link to home
Start Free TrialLog in
Avatar of rama_krishna
rama_krishna

asked on

How to capture the return value of the oracle function in shell script?

I have a shell script like this:

sqlplus -s $USER/$PASS << ENDSQL #1> /dev/null 2>&1
set define off
set head off
whenever sqlerror exit sql.sqlcode
select fn_get_current_date_for_region('USA') from dual;
 ENDSQL
   
I want to capture the return value of the oracle function fn_get_current_date_for_region('USA') in the shell script. How can i do this?
Thanks...
Avatar of glassd
glassd

One way to return a value is to echo it. Anything sent to STDOUT can be caught. Something like this:

#!/bin/ksh
GetHostName()
{
   typeset Host
   Host=$(uname -n)
   echo $Host
}

MyHost=$(GetHostName)

You can capture multiple values using this technique:

#!/bin/ksh
GetHostName()
{
   typeset Host OS
   Host=$(uname -n)
   OS=$(uname -s)
   echo $Host $OS
}

GetHostName | read MyHost MyOS

If you need to prompt for input inside the script, use this format:

read Var1?"Enter variable 1: "

This does not sent the prompt to STDOUT.

If you REALLY need to put data to the screen inside the function, try this:

#!/bin/ksh
GetHostName()
{
   typeset Host Screen
   Screen=$1
   echo "Gathering host info" > $Screen
   Host=$(uname -n)
   echo $Host
}

Term=$(tty)
MyHost=$(GetHostName $Term)

sqlplus -s $USER/$PASS << ENDSQL   >mylog.txt   2> /dev/null
set define off
set head off
whenever sqlerror exit sql.sqlcode
select fn_get_current_date_for_region('USA') from dual;
............add oracle statement to print "RETURN_VALUE IS :BLABLA"
 ENDSQL

At your ksh script

ORA_RET=`grep RETURN_VALUE mylog.txt  | cut -f2 -d":" `
echo $ORA_RET

Oops, I completely misread the question. What an idiot!!!
Probably better off doing this from Perl using the DBI module for Oracle (see www.cpam.org).

You'll have much better ability to get return values, error codes, row counts, etc., etc.
chris_calabrese:
Could you provide an exmaple for that? I want to learn that also
maybe i'm off base here, but would something like this work for your original question?

B_YOUR_VARIABLE=""
B_YOUR_VARIABLE=`sqlplus -s $LOGON_NAME/$LOGON_PASSWD @$HOME/YOUR_DIRECTORY/YOUR_SCRIPT.sql`

i use this to get the value returned by sql scripts into UNIX variables. just a thought....
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

PAQ  No refund

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

liddler
EE Cleanup Volunteer
ASKER CERTIFIED SOLUTION
Avatar of Computer101
Computer101
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial