The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
Google UNIX.COM


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
fuction return in perl jisha Shell Programming and Scripting 4 05-15-2008 01:51 AM
ORACLE return a function to Solaris Rafael.Buria Shell Programming and Scripting 1 10-23-2007 02:38 PM
Capture Oracle return code in shell script Vikas Sood Shell Programming and Scripting 1 05-22-2006 02:32 PM
how to return an array of elements from oracle to shell script satyakiran Shell Programming and Scripting 3 08-02-2005 06:57 AM
return code from oracle lesstjm Shell Programming and Scripting 3 01-29-2002 11:50 AM

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 04-14-2005
Registered User
 

Join Date: Sep 2004
Location: Mumbai-India
Posts: 151
Question Get Oracle fuction return value in a variable

Hi All,
I have the following code.

var=' '
sqlplus user/pass@DB <<EOF
whenever sqlerror exit 1
select package.func() into $var from dual;
EOF
echo $var

But, this code does not work to display the value returned by the oracle function. Do we have to bind variables before getting their values, like we do in PERL. Do Let me know.

Thanks.
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 04-14-2005
Registered User
 

Join Date: Sep 2004
Location: Mumbai-India
Posts: 151
Smile I got the soln, but if there's another way u can share.

Hi All,

I got the soln,

VAR=`sqlplus -silent user/pass@DB <<END
set pagesize 0 feedback off verify off heading off echo off
select package.func() from dual;
exit;
END`
if [ -z "$VAR" ]; then
echo "No rows returned from database"
exit 0
else
echo $VAR
fi

Thanks.
Reply With Quote
  #3 (permalink)  
Old 04-14-2005
google's Avatar
Moderator
 

Join Date: Jul 2002
Location: Atlanta
Posts: 740
This link shows a method for obtaining a value returned by a PL/SQL block. Click For the Link
Reply With Quote
  #4 (permalink)  
Old 04-14-2005
Registered User
 

Join Date: Jan 2005
Posts: 682
If you want more...

Not to beat a dead horse but if you want scads (more than one value) of stuff returned from Oracle you can utilize my favorite method:

Code:
#! /bin/ksh
...
# Create a SQL*Plus command processor co-process
sqlplus -s /nolog |&

# Connect to the database
print -p connect username/password@database

# Send SQL or PL/SQL block
print -p "
REM get_parameters()
SET HEAD OFF FEEDBACK OFF VERIFY OFF <- Set whatever formatting options you wish
COLUMN param FOLD_AFTER              <- This will cause all columns named param to be printed as individual rows
  SELECT 'Input_Record_Size:'   || input_record_size_no    param <- All columns are named param
        ,'Output_Record_Size:'  || output_record_size_no   param
        ,'Convert_to_ASCII:'    || convert_to_ascii_fl     param
        ,'Staging_Table:'       || load_table_name         param
        ,'SQLLoad_Control_File:'|| sqlload_control_file    param
    FROM load_parameters
   WHERE system_code  ='${SYSTEM_CD}' <- Pass whatever criteria you need
     AND file_type    ='${FILE_TYPE}'
ORDER BY load_priority
/
SET HEAD ON FEEDBACK ON VERIFY ON
COLUMN param CLEAR
PROMPT sql_complete <- This is very important.  If you don't print an arbitrary message that you can search for, the while loop below will hang while waiting for more information from Oracle
"

# Get responses back from Oracle
while read -p LINE
do
    # Look for our loop termination marker and break out when found
    if print $LINE | grep sql_complete
    then
        break

    # Process any Oracle errors
    elif print $LINE | grep Oracle error message(s)
    then
        print "Oracle error occurred"
        exit 1

    # Start setting variables based on return values
    elif print $LINE | grep "Input_Record_Size:"
    then
        INPUT_RECORD_SIZE=$(print $LINE | awk -F: '{print $2}')
        continue
    elif print $LINE | grep "Output_Record_Size:"
    then
        OUTPUT_RECORD_SIZE=$(print $LINE | awk -F: '{print $2}')
        continue
    ...more tests...
    fi
done

print "Input record size is: ${INPUT_RECORD_SIZE}"
print "Output record size is: ${OUTPUT_RECORD_SIZE}"
...
# Now a PL/SQL block
print -p "
REM Invoke stored procedure
SET HEAD OFF VERIFY OFF FEEDBACK OFF SERVEROUTPUT ON
DECLARE
    PRAGMA AUTONOMOUS_TRANSACTION;
    v_rc PLS_INTEGER;
BEGIN
    v_rc := please_raise_my_salary (${MY_ID});
    COMMIT;
    dbms_output.put_line('RC=:' || v_rc);
END;
/
SET HEAD ON VERIFY ON FEEDBACK ON SERVEROUTPUT OFF
PROMPT sql_complete
"

while read -p LINE
do
    if print $LINE | grep "sql_complete"
    then
        break
        
    ...same error tests as before...

    # Test for stored procedure return value
    elif print $LINE | grep "RC="
    then
        RETURN_VALUE=$(print $LINE | awk -F= '{print $2}')
        if [ 0 -eq ${RETURN_VALUE} ]
        then
            print "Finally, now I can buy another pair of shoes!"
        else
            print "Maybe I can have them re-soled"
        fi
    fi
done
I will keep the co-process available throughout my code and send SQL and PL/SQL blocks at various times.

Thomas

Last edited by tmarikle; 04-14-2005 at 10:03 AM.
Reply With Quote
Google UNIX.COM
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 01:15 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0