Get Oracle fuction return value in a variable


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Get Oracle fuction return value in a variable
# 1  
Old 04-14-2005
Question Get Oracle fuction return value in a variable

Hi All, Smilie
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.
# 2  
Old 04-14-2005
Bug 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.
Smilie
# 3  
Old 04-14-2005
This link shows a method for obtaining a value returned by a PL/SQL block. Click For the Link
# 4  
Old 04-14-2005
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 02:03 PM..
 
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX and Linux Applications

Identify a specific environment Oracle variable to connect a remote Oracle database ?

Good evening I nned your help pls, In an unix server i want to connect to a remote oracle databse server by sqlplus. I tried to find out the user/passwd and service name by env variable and all Ive got is this: ORACLE_SID_REPCOL=SCL_REPCOL ORACLE_SID=xmeta ORACLE_SID_TOL=SCL_PROTOLCOL... (2 Replies)
Discussion started by: alexcol
2 Replies

2. Shell Programming and Scripting

getting return value from oracle function

So, in Oracle (11gR2) on aix I have a function like: CREATE OR REPLACE function COMMON.t2(var1 in varchar2, vari in number,var2 in out number) return number as begin insert into korisnik_temp(kor_inicia, kor_opisno) values(1, var1); var2:=0; return var2; exception when... (12 Replies)
Discussion started by: bongo
12 Replies

3. UNIX and Linux Applications

Oracle return codes?

Having searched high and low through Oracles documentation, I came to think that they're very scripting-averse, as there's (apparently) no list of possible return/exit codes for their various command line utilities. Is anyone here in possession of such a list, or knows where to find one? It... (16 Replies)
Discussion started by: pludi
16 Replies

4. Shell Programming and Scripting

Insert a line including Variable & Carriage Return / sed command as Variable

I want to instert Category:XXXXX into the 2. line something like this should work, but I have somewhere the wrong sytanx. something with the linebreak goes wrong: sed "2i\\${n}Category:$cat\n" Sample: Titel Blahh Blahh abllk sdhsd sjdhf Blahh Blah Blahh Blahh Should look like... (2 Replies)
Discussion started by: lowmaster
2 Replies

5. Shell Programming and Scripting

Return rows from Oracle package

I need help . I am not getting anything back from my setup. I have defined an oracle package which I am calling from within my sql call, but is returning no rows. If I run my sql from sqlplus works find, does not work whe I try to run from UNIX shell script My korn script test.ksh #!bin/ksh... (6 Replies)
Discussion started by: TimHortons
6 Replies

6. UNIX for Dummies Questions & Answers

Return value from oracle to unix shell

Hi , i have written a shell scipt to call a procedure and get the value back from procedure.But i am facing the issue like #!/bin/sh returnedvalue=`sqlplus -s userid/pass<<EOF set serveroutput on; exec pass($1) set serveroutput off; EXIT; EOF` flag=`echo $returnedvalue ` echo "$flag"... (2 Replies)
Discussion started by: ravi214u
2 Replies

7. Shell Programming and Scripting

fuction return in perl

Hi All, I have a perl script(1.pl) that calls a c function defined in another file sample.c #!/usr/bin/perl my $re = 1; my @s = `/home/PERL_SCRIPTING/Rough/sample pline $re 10`; print "$_" foreach(@s); The sample.c is as bwlow: # include <stdio.h> int pline(int, int);... (4 Replies)
Discussion started by: jisha
4 Replies

8. Shell Programming and Scripting

ORACLE return a function to Solaris

Guys, I´m have this problem and I do not know what to do anymore: cod=`sqlplus -s ${DATABASE} << EOF set heading off feedback off verify off select max(eventid) from events; exit EOF` sed "s/CODEVENTID/${cod}/" c.ctl Abova What I´ve done. The CODEEVENTID is already set into a... (1 Reply)
Discussion started by: Rafael.Buria
1 Replies

9. Shell Programming and Scripting

return code from oracle

I am writing a unix script that logs into oracle and does a count on a table. I want to return that count to my unix script. this is my script: #!/bin/ksh typeset retcode; numin=0 sqlplus -s <<-EOSQL myuser/mypass@mydb variable ret_val number; begin ... (3 Replies)
Discussion started by: lesstjm
3 Replies
Login or Register to Ask a Question