简体   繁体   中英

How to capture the result of stored procedure through shell script?

I'm trying to execute stored procedure through shell script and try to get return from stored procedure but I didn't get any thing from the stored procedure on other hand same thing I do with sqlplus prompt and I'm able to get the result

sqlplus -silent xxx@xxx <<EOF
set serveroutput on
declare

DE_REC_COUNT number(10);
begin
    DE_DUP_PROC ('T_MCL_30404_20150317_020','MCL','30404','FT',DE_REC_COUNT);

end;

EOF

Through sqlplus prompt

SQL> set serveroutput on
declare

DE_REC_COUNT number;
begin
    DE_DUP_PROC ('T_MCL_30404_20150317_020','MCL','30404','FT',DE_REC_COUNT);

end;  

0

PL/SQL procedure successfully completed.

The version of the anonymous block in the shell script will not be executed as shown, because you don't have a slash after the block to run it . If you run that you get no output at all. If you change it to have a slash:

sqlplus -silent xxx@xxx <<EOF
set serveroutput on
declare
  DE_REC_COUNT number(10);
begin
    DE_DUP_PROC ('T_MCL_30404_20150317_020','MCL','30404','FT',DE_REC_COUNT);
end;
/
EOF

then you'll see:

0

PL/SQL procedure successfully completed.

You've shown the interactive version in SQL*Plus without the slash too, but you must have had that to see the output you showed.

If you want the zero - which seems to be coming from a dbms_output call in your procedure, rather than directly from your anonymous block - na shell variable you can refer to later, you can assign the output of the heredoc to a variable:

MY_VAR=`sqlplus -silent xxx@xxx <<EOF
set serveroutput on
set feedback off
declare

DE_REC_COUNT number(10);
begin
    DE_DUP_PROC ('T_MCL_30404_20150317_020','MCL','30404','FT',DE_REC_COUNT);
end;
/

EOF`

printf "Got back MY_VAR as %s\n" ${MY_VAR}

Note that I've added set feedback off so you don't see the PL/SQL procedure successfully completed line. Now when you run that you'll see:

Got back MY_VAR as 0

and you can do whatever you need to with ${MY_VAR} . It depends what you mean by 'capture' though.

Here's an example of how it can be done by surrounding the code with the evaluation operators (` back quotes):

#!/bin/sh
results=`sqlplus -s xxx@xxx <<EOF
set serveroutput on feedback off
declare

DE_REC_COUNT number(10);
begin
    DE_DUP_PROC ('T_MCL_30404_20150317_020','MCL','30404','FT',DE_REC_COUNT);

end;
/
EOF`

echo $results

Lets say for instance I have a below procedure which calculates the sum of two numbers and prints the total using dbms_output.put_line() method

CREATE OR REPLACE
PROCEDURE SUM
  (
    a IN NUMBER,
    b IN NUMBER)
AS
  total NUMBER;
BEGIN
  total:= a + b;
  dbms_output.put_line(total);
END;

Then in order to call this in our shell script we need to call this procedure in an anonymous block and store in a variable, later using echo we can execute the command.

plsqlcode=`sqlplus -silent hr/sandeep@orcl <<EOF
set serveroutput on
begin
sum(10,20);
end;
/
EOF`

echo $plsqlcode

Ideally this is not a recommended approach you should keep your shell and pl/sql code in separate files.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM