【问题标题】:Get return value of a SQL function in ksh在 ksh 中获取 SQL 函数的返回值
【发布时间】:2015-11-19 11:36:15
【问题描述】:

我正在使用 KSH 调用 SQL 函数。根据函数的输出/返回值,我想做一些检查,但首先我需要访问该值。

这是我的 KSH 代码/功能:

# Call runSeriatimValuation Web Service
runSeriatimValuation() {

  jobNum=$1
  echo "Running Seriatim Valuation with ${jobNum}"

  # Call web service Oracle stored procedure
  ${sqlplusCmd} << END_SQLPLUS  
   WHENEVER SQLERROR EXIT SQL.SQLCODE ROLLBACK;
   WHENEVER OSERROR EXIT 9 ROLLBACK;
   DECLARE
    v_Return INT;
   BEGIN
    v_Return := CALL_IPVFBJAVA_WEBSERVICE(${jobNum});
    COMMIT;
   END;
   /
END_SQLPLUS

# This is the return value of the SQL command above if it was 
# successfully execute or not. Its not related to v_Return.

 errorCode=$?

 # If error, handle and exit
 if [ "${errorCode}" -ne "0" ] ; then
   echo "Error (${errorCode}) occurred in runSeriatimValuation when calling procedure"
   exitAgent 1
 fi

# **** Here I would like to do my if/else checking on v_Return, How can I do that ???? ***** 
# how can I assign v_Return to a variable of type int and check the value of it.

 return 0
}

谢谢 - 非常感谢您的帮助。

【问题讨论】:

  • 您应该将if [ "${errorCode}" -ne "0" ] 更改为if [ ${errorCode} -ne 0 ] (比较整数)或if [ "${errorCode}" != "0" ] (比较字符串)。我不知道这是否能回答所有问题,您能否在 if 语句之前 echo "errorCode=${errorCode}" 并使用正确和不正确的代码进行测试(将 COMMIT 更改为 AJR)。
  • 谢谢 - 好收获。但是如何检查 v_Return 值?
  • 你能在COMMIT 之后做类似EXIT v_Return 的事情吗?也许用EXIT 42 进行测试,这样你就会得到一个清晰的返回码。

标签: sql shell unix plsql ksh


【解决方案1】:

你可以像下面这样直接查看返回类型,

if [ $? -eq 0 ]
then
##DO Something
else
##DO Something
fi

【讨论】:

  • 是的,我可以检查返回类型,但这将是执行的 SQL 命令的返回类型。我在上面的脚本中正在做的事情。我想要的是检查 v_Return 的值。那就是我遇到问题的地方。谢谢
【解决方案2】:

我所做的是将 SQL 返回的值放入一个数组中,然后获取数组中的第一个元素来检查它。

代码如下:

set -A fnOut `${sqlplusCmd} << END_SQLPLUS 
WHENEVER SQLERROR EXIT SQL.SQLCODE ROLLBACK;
WHENEVER OSERROR EXIT 9 ROLLBACK;
SET PAGESIZE 0 FEEDBACK OFF VERIFY OFF HEADING OFF ECHO OFF;
SELECT IPVOWN.CALL_IPVFBJAVA_WEBSERVICE(${jobNum}) FROM DUAL;
END_SQLPLUS`

 # Get return value
 errorCode=$?
 echo "errorCode = ${errorCode}"

 # If error, handle and exit
 if [ "${errorCode}" -ne "0" ] ; then
   echo "Error (${errorCode}) occurred in runSeriatimValuation when calling procedure"
  exitAgent 1
 fi

echo "First element in fnOut is ${fnOut[0]}"
 # If job found to be cancelled then return 1, otherwise return 0
 if [ "${fnOut[0]}" -ne "0" ] ; then
  echo "PL/SQL Procedure Failed, The return value is ${fnOut[0]}"
  exitAgent -1
 fi

希望这会有所帮助。

谢谢,

【讨论】:

    【解决方案3】:

    您可以做的是设置一个变量并将其等同于 sql 命令。

    例如:

    var1=${sqlplusCmd} << END_SQLPLUS  
    WHENEVER SQLERROR EXIT SQL.SQLCODE ROLLBACK;
    WHENEVER OSERROR EXIT 9 ROLLBACK;
    select CALL_IPVFBJAVA_WEBSERVICE(${jobNum}) from dual;
    commit
    exit;
    END_SQLPLUS
    

    【讨论】:

      猜你喜欢
      • 2012-02-11
      • 2012-02-22
      • 2019-01-20
      • 1970-01-01
      • 2011-11-25
      • 1970-01-01
      • 1970-01-01
      • 2016-01-03
      • 1970-01-01
      相关资源
      最近更新 更多