how to capture oracle function returning 2 values in unix


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting how to capture oracle function returning 2 values in unix
# 1  
Old 04-05-2010
how to capture oracle function returning 2 values in unix

i have an oracle function which returns two values, one is the error message if the function encounters anything and another one which returns a number

i need to capture both
and pass it on to unix shell script

how to do it
# 2  
Old 04-05-2010
Quote:
Originally Posted by trichyselva
i have an oracle function which returns two values, one is the error message if the function encounters anything and another one which returns a number
Do you mean an Oracle procedure ?
An Oracle function can return only a single value to the caller. The return value is strongly typed via the RETURN keyword, which means you can only return a number.
The error message you are talking about is thrown in case of an exception. It is not a return value.

Quote:
i need to capture both
and pass it on to unix shell script
...
If you leave out the exception section from the function, then maybe something like this -

Code:
$
$ cat -n get_tvalue.sh
     1  #!/usr/bin/bash
     2  x=$(sqlplus -s /nolog <<EOF
     3  connect <login_credentials>
     4  set pages 0 feedback off
     5  var n varchar2(10)
     6  exec :n := <your_function>;
     7  print n
     8  exit
     9  EOF)
    10  echo "x => $x"
    11
$
$

should assign the returned number or the error message to x.
Here <login_credentials> are your Oracle login credentials in the form username/password@SID and
<your_function> is the invocation of your function that returns a number.

HTH,
tyler_durden

Last edited by durden_tyler; 04-05-2010 at 10:58 AM..
# 3  
Old 04-06-2010
IO

As Durden indicates functions typically return a value. Even in languages such as Perl which can return arrays and lists they still return these values as a single entity.

With that said I'd like to ask a question. Do you know if the error value comes back from STDERR because the Oracle command fails?

Rather than waiting for an answer I'll assume the above to be the case where an error message is written to STDERR and the command returns a non-zero value as status. Here's some test code. The first script, 'test9a.sh', calls a second script, 'test9b.sh', which can return an error. The error is returned if you specify 'error' as the first parameter to the first script.
Code:
#!/bin/bash
# script test9a.sh

sfile=$(basename $0)

#echo "$sfile: start"
#echo "$sfile: param: $1"

err=""
rval=$(./test9b.sh $1 2>&1 )
rcode=$?
if [ $rcode -ne 0 ]; then 
    err="$rval"
    rval=""
fi
echo "$sfile: rcode=$rcode, rval=\"$rval\", err=\"$err\""

Code:
#!/bin/bash
#script test9b.sh

sfile=$(basename $0)

#echo "$sfile: start"
#echo "$sfile: param: $1"
if [ "$1" == "error" ]; then
    echo "$sfile: Error message" >&2
    exit 1
fi
echo "$sfile: Return Value $1"
exit 0

Try it out to see if these scripts emulate your situation.

Last edited by m1xram; 04-06-2010 at 08:39 PM.. Reason: Add scripts and answer
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Returning and capturing multiple return values from a function

Hi I am pretty confused in returning and capturing multiple values i have defined a function which should return values "total, difference" i have used as #!/usr/bin/ksh calc() { total=$1+$2 echo "$total" diff=$2-$1 echo "$diff" } I have invoked this function as calc 5 8 Now i... (2 Replies)
Discussion started by: Priya Amaresh
2 Replies

2. Programming

Function Returning Pointer

Hi guys. how a functions such fdopen, ... can return pointer? are these functions use static memory(variables)? (6 Replies)
Discussion started by: majid.merkava
6 Replies

3. Shell Programming and Scripting

unix capture oracle function error

Hi, I want to execute an oracle function from unix script so for that I created a sample oracle function as below: create or replace function test_fn(test_date out varchar2) RETURN varchar2 IS BEGIN select to_char(sysdate,'DD-MON-YY') into test_date from dual; return test_date;... (5 Replies)
Discussion started by: dips_ag
5 Replies

4. Shell Programming and Scripting

how to pass the values to unix shell from the oracle stored procedure.

Hi i am calling a stored procedure from unix shell like this call test_proc('0002','20100218'); the stored procedure was giving output like this dbms_output.put_line(' processed earlier'); i want to see the output in the unix shell where i called. Thanks barani (6 Replies)
Discussion started by: barani75
6 Replies

5. Shell Programming and Scripting

Returning the name of function used

Hi All In my script, I can call on several functions. I have a logging function that is called by any of these functions. What I would like is some way of identifying which function I am using and pass this to the log function as some parameter. Is there some built in command or way of... (3 Replies)
Discussion started by: kingpin2502
3 Replies

6. Programming

returning multiple values from a function in C

hi how can I return multiple values from a C function. I tried the following: #include <stdio.h> void foo(int id, char *first_name, char *last_name) { /* this is just an example to illustrate my problem... real code makes use of the "id" parameter. */ first_name = (char... (8 Replies)
Discussion started by: Andrewkl
8 Replies

7. Shell Programming and Scripting

returning from a function

Hi all, I am very new to BASH shell programming. I need to return an integer from a function to the caller function. I did this: but it keeps giving me wrong return: Can someone help me out here, please? Thanks (2 Replies)
Discussion started by: alirezan
2 Replies

8. Shell Programming and Scripting

Values from Oracle to Unix

Hello Experts how do I get a output value from Oracle to Unix . Please give me sample Unix codings Scenario (Filne name : test.sh <argument > ) --------- 1. Passing argument from unix to sql 2. connecting to Oracle & from oracle get output 3. Pass the output to ... (3 Replies)
Discussion started by: Krishnaramjis
3 Replies

9. Programming

string returning function

I have two string returning function in ESQL/C char *segment_name(lbuffer) char *lbuffer; {..... and char *get_bpdvalue(f_name) char *f_name; {...... both declared above main() char *get_bpdvalue(); char *segment_name(); my problem is segment_name works on sprintf and strcpy... (5 Replies)
Discussion started by: jisc
5 Replies

10. UNIX for Dummies Questions & Answers

how to pass values from oracle sql plus to unix shell script

how to pass values from oracle sql plus to unix shell script (2 Replies)
Discussion started by: trichyselva
2 Replies
Login or Register to Ask a Question