Shell script to catch PL/SQL return values


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Shell script to catch PL/SQL return values
# 1  
Old 03-23-2010
Question Shell script to catch PL/SQL return values

I tried searching the forum for similar posts but its closed now.
Would appreciate any help on this.
I am trying to capture return value from a select query into a variable.
DB is Oracle
I am able to spool it to a file but I donot intend to use it.

Here is my script that does not work Smilie
I am able to connect to the db but the value is not populated into variable
Code:
 #!/bin/sh
ORACLE_HOME=/opt/oracle/product/9.2.0
ORACLE_SID=$1
echo "Date is Fridayyyyy: $TODAY"
x = '$ORACLE_HOME/bin/sqlplus report/report@$ORACLE_SID <<EOF
declare 
var number;
select count(*) from report.CUBE_BUILD_INDICATOR;
dbms_output.put_line(var);
exit;
eof'
echo 'done 1'
echo $var
echo var
echo 'done 2'
echo x
echo $x


Last edited by jim mcnamara; 03-23-2010 at 12:49 PM.. Reason: code tags
# 2  
Old 03-23-2010
lots problems that I can see quickly:
Code:
 #!/bin/sh
ORACLE_HOME=/opt/oracle/product/9.2.0
ORACLE_SID=$1
echo "Date is Fridayyyyy: $TODAY"
x=$($ORACLE_HOME/bin/sqlplus report/report@$ORACLE_SID <<EOF
SET SERVEROUT ON SIZE 100000
declare 
  var number:=0;
BEGIN
  dbms_output.enable(100000);
  select count(*) 
    into var 
   from report.CUBE_BUILD_INDICATOR;
  dbms_output.put_line(var);
END;
/
exit;
EOF )
echo 'done 1'
echo var
echo $var
echo 'done 2'
echo x
echo $x

you need to mind spaces and capitalization; x=$( ) runs a process as a child and assigns the printed output to x;
SET SERVEROUT allows dbms_output to write to the tty
dbms_out.enable() is also required
EOF <-> EOF must match casewise
you need "select something into var" in order for var to have a value.
You must have gotten dozens of errors - both from PL/SQL and from shell.
# 3  
Old 03-23-2010
Thanks for the prompt reply. Variable still displays nulls. Is thr something major i am missing.
( Am a total novice in this )
# 4  
Old 03-23-2010
reread the post, please, copy it EXACTLY, don't add or remove spaces, or change case.
If that fails, get into sqlplus, cut and paste the PL/SQL stuff (from SET SERVEROUT ... to
Code:
END;
/

)onto the screen and run it.

---------- Post updated at 10:05 ---------- Previous update was at 10:03 ----------

I tried the pl/sql it works correctly as posted.
# 5  
Old 03-23-2010
Yeah the code works gud. But the value of x and var turns out null.
Any idea why.. infact var should atleast be 0 since its been initialized
# 6  
Old 03-23-2010
Quote:
Originally Posted by monie2717
Yeah the code works gud. But the value of x and var turns out null.
Any idea why.. infact var should atleast be 0 since its been initialized
Firstly, the variable "var" is declared in the anonymous PL/SQL block. It is not a shell variable in the caller environment. So, I'd think its scope would be local to the PL/SQL block, and you wouldn't see it in the Unix script.

Secondly, in order to set the value of "x", you may want to run sqlplus in "silent" mode. And also set a few SQL*Plus report environment parameters so as to ensure that only the value of "var" is printed. (Other stuff like the blurb, the feedback message etc. should not be printed.)

Try this -

Code:
#!/usr/bin/sh
ORACLE_HOME=/opt/oracle/product/9.2.0
ORACLE_SID=$1
echo "Date is Fridayyyyy: $TODAY"
x=$($ORACLE_HOME/bin/sqlplus -s report/report@$ORACLE_SID <<EOF
set heading off pages 0 feedback off
SET SERVEROUT ON SIZE 100000
declare 
  var number:=0;
BEGIN
  select count(*) 
    into var 
   from report.CUBE_BUILD_INDICATOR;
  dbms_output.put_line(var);
END;
/
exit;
EOF )
echo 'done 1'
echo var
echo $var
echo 'done 2'
echo x
echo $x

HTH,
tyler_durden
# 7  
Old 03-23-2010
it does not work .. can this be achieved without a pl sql block
and using just a select query and a linux variable .
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to catch sql error in script?

Hi Gurus, I have a script which send sql query to oracle db and return value to my script. dummy code like below: sqlplus -s user/${PASSWD}@${ORACLE_SID} @${DIR}/query.sql > outputfile using above code, when query has error, it send error to same out put file and exit code is 0, is... (6 Replies)
Discussion started by: ken6503
6 Replies

2. Shell Programming and Scripting

Return value to shell script, depending on status of pl/sql udpate

Hi All, I need to return value to the main shell script, depending on whether the UPDATE command in the embedded pl/sql is successfu or not. #!bin/ksh updateStatus=`sqlplus --conn details-- << EOF DECLARE var_rows NUMBER; BEGIN update table_name set column_name =... (7 Replies)
Discussion started by: rituparna_gupta
7 Replies

3. Shell Programming and Scripting

Query the table and return values to shell script and search result values from another files.

Hi, I need a shell script, which would search the result values from another files. 1)execute " select column1 from table_name" query on the table. 2)Based on the result, need to be grep from .wft files. could please explain about this.Below is the way i am using. #!/bin/sh... (4 Replies)
Discussion started by: Rami Reddy
4 Replies

4. UNIX for Advanced & Expert Users

Call parallel sql scripts from shell and return status when both sql are done

Hi Experts: I have a shell script that's kicked off by cron. Inside this shell script, I need to kick off two or more oracle sql scripts to process different groups of tables. And when both sql scripts are done, I will continue in the shell script to do other things like checking processing... (3 Replies)
Discussion started by: huasheng8
3 Replies

5. Shell Programming and Scripting

Assigning return value of an embedded SQL in a shell script variable

I've a script of the following form calling a simple sql that counts the no of rows as based on some conditions. I want the count returned by the sql to get assigned to the variable sql_ret_val1. However I'm finding that this var is always getting assigned a value of 0. I have verified by executing... (1 Reply)
Discussion started by: MxC
1 Replies

6. Shell Programming and Scripting

calling pl/sql procedure from shell and return values

How could I call an Oracle PL/SQL procedure from any shell (bash) and catch returning value from that procedure (out param) or get a returning value if it's a function. also, I got into trouble when I tried to send a number as a param #!/bin/bash -e username=$1 pwd=$2 baza=$3... (0 Replies)
Discussion started by: bongo
0 Replies

7. Shell Programming and Scripting

how to store the return values of stored procedure in unix shell script.

hi i am calling a oracle stored procedure(in the database) from unix shell scripting (a.sh). the called stored procedure returns some values through OUT variables i want to assign the return values of stored procedure in to unix shell script variable. can you provide me the code. ... (1 Reply)
Discussion started by: barani75
1 Replies

8. Shell Programming and Scripting

Need to return fail or pass from shell script on the basis of pl/sql code execution

Hi guys, I am quite new in shell scripting. I am tring to promote some oracle jobs into control-M. In control-M, I am calling a script which establishes a connection with database and execute some procedures. Now I want if that PL/sql Block got failed script should return failure to... (2 Replies)
Discussion started by: alok1301
2 Replies

9. Shell Programming and Scripting

Shell script to catch PL/SQL return values

Hello, I need some help from the experts on PL/SQL and Shell scripting. I need a shell script that runs a PL/SQL procedure and gets the values returned from the PL/SQL procedure into the shell variables. The PL/SQL procedure returns multiple values. I was able to assign a single return value... (1 Reply)
Discussion started by: Veera_Raghav
1 Replies

10. Shell Programming and Scripting

passing values from sql to shell script

Hi guyz, Posting a thread after a long time. I want to pass two variables to unix shell script from sql script. Note: I am calling sql script from unix script. sql script has 2 variables one is the return code for status of program run and second one email flag. I don't know how to capture... (3 Replies)
Discussion started by: sachin.gangadha
3 Replies
Login or Register to Ask a Question