getting return value from oracle function


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting getting return value from oracle function
# 8  
Old 04-23-2010
Hmm. What command line did you type, and what was the reply (including any error messages)?


Noticed that this line could give trouble. The shell variable $var1 will not get substitued because it is between single quote characters.

Code:
       execute retval = $procedure('$var1', $vari, :var2);


Last edited by methyl; 04-23-2010 at 11:04 AM..
# 9  
Old 04-23-2010
Quote:
Originally Posted by bongo
So, in Oracle (11gR2) on aix I have a function like:

Code:
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 others then
    var2:=-1;
    return var2;
end;

...
Before you code anything in Oracle further, I'd suggest you to go through the "Oracle Concepts Guide", especially the section on Oracle subprograms - functions, procedures and packages.

An Oracle function *ALWAYS* returns a value via the RETURN statement. The datatype of the return value is strongly typed via the RETURN clause of its signature.

See below -

Code:
test@ORA11G>
test@ORA11G> --
test@ORA11G> -- create an Oracle function with an OUT parameter
test@ORA11G> --
test@ORA11G> create or replace function f1 (x out number)
  2  return number
  3  is
  4  begin
  5    return 100;
  6  end;
  7  /
Function created.
test@ORA11G>
test@ORA11G> -- now test it
test@ORA11G> declare
  2    p number;
  3    q number;
  4  begin
  5    p := f1(q);
  6    dbms_output.put_line('q = '||q);
  7  end;
  8  /
q =
PL/SQL procedure successfully completed.
test@ORA11G>
test@ORA11G> -- so we *CANNOT* return a function's value via
test@ORA11G> -- the OUT parameter mode
test@ORA11G> -- let's try again
test@ORA11G> declare
  2    p number;
  3    q number;
  4  begin
  5    p := f1(q);
  6    dbms_output.put_line('p = '||p);
  7  end;
  8  /
p = 100
PL/SQL procedure successfully completed.
test@ORA11G>
test@ORA11G> -- as you can see, the return value is always
test@ORA11G> -- from an Oracle function, via the "RETURN" statement
test@ORA11G> -- And a function can *ONLY* return single value
test@ORA11G>
test@ORA11G> -- In fact, the "OUT" parameter mode is useless !
test@ORA11G> -- Also, "IN" need not be mentioned, because all parameters are "IN" parameters
test@ORA11G> create or replace function f1 (x number)
  2  return number
  3  is
  4  begin
  5    -- do your stuff here
  6    return 2*x;
  7  end;
  8  /
Function created.
test@ORA11G>
test@ORA11G> -- and test it now
test@ORA11G> declare
  2    q number := 10;
  3    p number;
  4  begin
  5    p := f1(q);
  6    dbms_output.put_line('p = '||p);
  7  end;
  8  /
p = 20
PL/SQL procedure successfully completed.
test@ORA11G>
test@ORA11G>
test@ORA11G>

HTH,
tyler_durden

Last edited by durden_tyler; 04-23-2010 at 11:46 AM..
# 10  
Old 04-23-2010
Quote:
Originally Posted by methyl
Hmm. What command line did you type, and what was the reply (including any error messages)?


Noticed that this line could give trouble. The shell variable $var1 will not get substitued because it is between single quote characters.

Code:
       execute retval = $procedure('$var1', $vari, :var2);

this part is not a problem, because it was working as a procedure


shell scripts returns:


BEGIN retval := t2('aaa', 22, :var2); END; 0 connections connections_sys cr_46995 ne out.ll out.log out_sys.log run.sh run_sys.sh shell_skripta sql_skripta sql_skripta~ sqlnet.log sqlplus.sh sqlplus.sh~ sqlplus2.sh temp test test~ ERROR at line 1: ORA-06550: line 1, column 7: PLS-00201: identifier 'RETVAL' must be declared ORA-06550: line 1, column 7: PL/SQL: Statement ignored
06550
# 11  
Old 04-23-2010
See my earlier post, you should change your function's signature.
Shown below is how you should capture the function's return value, but this is still lousy code - pass only those parameters that you need to.

Code:
$
$
$ cat -n process.sh
     1  #!/bin/bash -e
     2  username=$1
     3  baza=$2
     4  procedure=$3
     5  var1=$4
     6  prosao=0
     7  passwd=''
     8  vari=22
     9  retval=0
    10  #echo $vari
    11  while read line
    12  do
    13          if [[ ! "$line" =~ ^# ]]; then
    14                  tns="$(echo $line | cut -d: -f1)"
    15                  login="$(echo $line | cut -d: -f2)"
    16                  pwd="$(echo $line | cut -d: -f3)"
    17                  if [ $login = $username ]
    18                  then
    19                          if [ $tns = $baza ]
    20                          then
    21                                  passwd=$pwd
    22                                  prosao=1
    23                          fi
    24                  fi
    25          fi
    26  done < "connections"
    27  if [ $prosao = 1 ]
    28  then
    29          vvarr=$(sqlplus -s -l $username/$passwd@$baza<<END
    30          set heading off feedback off pages 0
    31          var var2 Number;
    32          var retval Number;
    33          execute :retval := $procedure('$var1', $vari, :var2);
    34          print :retval;
    35          commit;
    36          exit;
    37          END)
    38          echo "vvarr => $vvarr"
    39  else
    40          echo -2
    41  fi
$
$ ./process.sh test ora11g t2 xyz
vvarr =>         0
$
$

tyler_durden
# 12  
Old 04-23-2010
great, it's working that way Smilie

also a question...
what if $procedure points to a wrong proc. name?
how could I put begin.. exception..end block in this code?
# 13  
Old 04-23-2010
Quote:
Originally Posted by bongo
great, it's working that way Smilie ...
Yes, but I do hope you understand it still is not a good way of doing things.
- You should remove that OUT parameter from the function, because it is useless.
- Remove the 2nd parameter since it is never used inside your function.
- And once you do your DML stuff, you can return the hard-coded value 0 on success.
- Also capturing "when others then" will only suppress the line number that threw the exception. A better way is to capture only named exceptions, or the ones that you expect. If you are unsure of the exceptions that will be thrown, then leave out "when others then" and let Oracle do the escalation part for you. The resultant diagnostic information will be much more useful than what you have now.

Quote:
also a question...
what if $procedure points to a wrong proc. name?
how could I put begin.. exception..end block in this code?
[/quote]

You cannot put that in a begin...end block.
Invocation of procedure is in the sqlplus environment itself.
And the "ORA-" message is returned to the sqlplus environment.
The begin...end block does not even come into play in this scenario.

tyler_durden
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Function - Make your function return an exit status

Hi All, Good Day, seeking for your assistance on how to not perform my 2nd, 3rd,4th etc.. function if my 1st function is in else condition. #Body function1() { if then echo "exist" else echo "not exist" } #if not exist in function1 my all other function will not proceed.... (4 Replies)
Discussion started by: meister29
4 Replies

2. Shell Programming and Scripting

Return: can only `return' from a function or sourced script

Not sure where the problem is. I can run the script without any issue using the following command. . /opt/app/scripts/cdc_migration.sh But it fails with the below error when I try it this way /opt/app/scripts/cdc_migration.sh /opt/app/scripts/cdc_migration.sh: line 65: return: can only... (1 Reply)
Discussion started by: svajhala
1 Replies

3. Shell Programming and Scripting

Return a value from called function to the calling function

I have two scripts. script1.sh looks -------------------------------- #!/bin/bash display() { echo "Welcome to Unix" } display ----------------------------- Script2.sh #!/bin/bash sh script1.sh //simply calling script1.sh ------------------------------ (1 Reply)
Discussion started by: mvictorvijayan
1 Replies

4. Shell Programming and Scripting

return in function

I am using ksh. I want to know how can we make any function to return string or double value. I dont want to use the global variables. (5 Replies)
Discussion started by: PRKS
5 Replies

5. Shell Programming and Scripting

return value of a function

I have write a shell function to get the maximum of a vector. However, the returned value from the function is not always the correct one. Here is the script: maxval() { local max j i size arrval size=$1 ; shift max=-999999999 i=0 while do arrval="$1" if then ... (5 Replies)
Discussion started by: fl0r10
5 Replies

6. Shell Programming and Scripting

need to return value from function

how to return value from function and print from main program??? And also I need to return true or false... Is it possible? (5 Replies)
Discussion started by: darshakraut
5 Replies

7. Shell Programming and Scripting

function return array

Hi all I would like to know if there is a way to return an array for a function. As I know function can return all the contents in an array, I want to return an array type. (6 Replies)
Discussion started by: dophine
6 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 value of a function

Hi I have a doubt in the way the variables inside a function are treated . if a function is called from the main script directly, the variables inside them act as global variables. however if the return value of the function is stored to some other variable in the main script as shown,... (3 Replies)
Discussion started by: prez
3 Replies
Login or Register to Ask a Question