简体   繁体   English

SQLPLUS输出到shell脚本而不返回值

[英]SQLPLUS output to shell script not returning value

I'm trying to return a sqlplus output to a shell script. 我正在尝试将sqlplus输出返回到shell脚本。 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: 这是我的pl / sql脚本:

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: 这是我的shell脚本:

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

$RETVAL always returns 0 even when I have X_RETURN_CODE := 5 即使我有X_RETURN_CODE:= 5,$ RETVAL也总是返回0

X_RETURN_CODE has no meaning outside of the scope of the PL/SQL block where it is declared. X_RETURN_CODE在声明它的PL / SQL块的范围之外没有任何意义。 You need to use a SQLPlus bind variable. 您需要使用SQLPlus绑定变量。

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

我的猜测是END标记块的结束并且X_RETURN_CODE超出范围,因此它默认为0.或者您可能应该使用RETURN语句而不是EXIT。

$ sqlplus -s <<!
> / as sysdba
> @tester
> !

PL/SQL procedure successfully completed.

$ echo $?
5

$ cat tester.sql
SET SERVEROUTPUT ON

var X_RETURN_CODE number

DECLARE
X_RETURN_MSG VARCHAR2(32767);

BEGIN
X_RETURN_MSG := NULL;
:X_RETURN_CODE := 5;

COMMIT;
END;
/

EXIT :X_RETURN_CODE;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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