Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQLPLUS output to shell script not returning value

I'm trying to return a sqlplus output to a shell script. This may sound simple enough but I've searched online for some time and cannot get my script to work.

Here is my pl/sql script:

SET SERVEROUTPUT ON

DECLARE 
X_RETURN_MSG VARCHAR2(32767);
X_RETURN_CODE NUMBER;

BEGIN 
X_RETURN_MSG := NULL;
X_RETURN_CODE := 5;

COMMIT;
END;

EXIT X_RETURN_CODE;

Here is my shell script:

sqlplus -s user/pwd <<EOF
@../sql/tester.sql
EOF
RETVAL=$?
echo $RETVAL

$RETVAL always returns 0 even when I have X_RETURN_CODE := 5

like image 977
user965422 Avatar asked Mar 28 '26 21:03

user965422


1 Answers

X_RETURN_CODE has no meaning outside of the scope of the PL/SQL block where it is declared. You need to use a SQLPlus bind variable.

SQL> VARIABLE return_code NUMBER
SQL> BEGIN
  2    :return_code := 5;
  3  END;
  4  /

PL/SQL procedure successfully completed.

SQL> EXIT :return_code
Disconnected from Oracle Database 10g Release 10.2.0.4.0 - Production
> echo $?
5
like image 132
Dave Costa Avatar answered Apr 02 '26 11:04

Dave Costa



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!