Home » SQL & PL/SQL » SQL & PL/SQL » How to return values from PL/SQL to UNIX shell variables?
How to return values from PL/SQL to UNIX shell variables? [message #38893] Fri, 24 May 2002 02:14 Go to next message
Joerg Romswinkel
Messages: 1
Registered: May 2002
Junior Member
Hello,

does anyone know if it's possible to return one or more values from PL/SQL variables back to the calling UNIX shell script to assign them to shell variables?

Here is what I try:

sqlplus -s /nolog <<- EOF
connect $EXPORT_DB_CONNECT
set serveroutput on

DECLARE
n1 NUMBER(3);
n2 NUMBER(3);
BEGIN
n1 := export.my_function1();
n2 := export.my_function2();
END;
/

disconnect /* Disconnect from Oracle */
exit ;
EOF

I need the values from n1 and n2 in my shell script.

Thanks in advance.

Joerg
Re: How to return values from PL/SQL to UNIX shell variables? [message #38895 is a reply to message #38893] Fri, 24 May 2002 02:33 Go to previous messageGo to next message
oxkar
Messages: 9
Registered: May 2002
Junior Member
You could use a procedure to write the variables in a file and then read them from there or excute a command to export the variables to enviroment variables on your Unix.

The following link describes how to execute commands from a procedure http://www.orafaq.net/scripts/c_src/extproc.txt
Re: How to return values from PL/SQL to UNIX shell variables? [message #38897 is a reply to message #38893] Fri, 24 May 2002 03:40 Go to previous messageGo to next message
Oskar
Messages: 26
Registered: May 2002
Junior Member
How to export the pl/sql variables to enviroment variables ?
Re: How to return values from PL/SQL to UNIX shell variables? [message #38906 is a reply to message #38893] Fri, 24 May 2002 17:49 Go to previous message
andrew again
Messages: 2577
Registered: March 2000
Senior Member
play with this code (just change ~~ to two '<').
#!/bin/ksh
#======================================================
# stored proc with parms
#======================================================
## CREATE OR REPLACE PROCEDURE p1 (i_parm IN NUMBER, o_parm OUT NUMBER)
## IS
## BEGIN
##    o_parm := 5 * i_parm;
## END;
## /
my_in_parm=5
echo =============================
echo FIRST
echo =============================
RETVAL=`sqlplus -s scott/tiger@dev ~~EOF
set serveroutput on
WHENEVER SQLERROR EXIT 1
declare
x number := $my_in_parm;
y number;
begin
p1(x, y);
dbms_output.put_line('o_parm from p1 is '||y);
end;
/
exit;
EOF`

echo $RETVAL
X=`echo $RETVAL | awk '{print $5}'`
echo $X

## CREATE OR REPLACE PROCEDURE p2 (i_parm IN NUMBER, o_parm OUT NUMBER)
## IS
## BEGIN
##    o_parm := 5 * i_parm;
##    DBMS_OUTPUT.put_line ('Line one');
##    DBMS_OUTPUT.put_line ('Line two');
## END;
## /
my_in_parm=5
echo =============================
echo SECOND
echo =============================
set -A my_arr `sqlplus -s scott/tiger@dev ~~EOF
set serveroutput on
WHENEVER SQLERROR EXIT 1
declare
x number := $my_in_parm;
y number;
begin
p2(x, y);
dbms_output.put_line('o_parm from p1 is '||y);
end;
/
exit;
EOF`

echo "there are ${#my_arr[*]} elements in the array"
element=0
while [ $element -lt  ${#my_arr[*]} ]
  do
  echo "==>"${my_arr[$element]}
  let element=$element+1;
done

echo "Echo all in one command now!"
echo  ${my_arr[*]}

>t.ksh

=============================
FIRST
=============================
o_parm from p1 is 25 PL/SQL procedure successfully completed.
25
=============================
SECOND
=============================
there are 13 elements in the array
==>Line
==>one
==>Line
==>two
==>o_parm
==>from
==>p1
==>is
==>25
==>PL/SQL
==>procedure
==>successfully
==>completed.
Echo all in one command now!
Line one Line two o_parm from p1 is 25 PL/SQL procedure successfully completed.

Previous Topic: very urgent- calling SQLPLUS from PL/sql
Next Topic: How to generate test data in PLSQL
Goto Forum:
  


Current Time: Tue May 21 19:45:08 CDT 2024