db2 - 如何在shell中获取存储过程OUT型参数的返回值(awk)

Posted t0nsha

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了db2 - 如何在shell中获取存储过程OUT型参数的返回值(awk)相关的知识,希望对你有一定的参考价值。

示例:

OUT_MSG=$(db2 -v "call liao.proc1('$PAR1','$PAR2',?)" )
CMDRET=$?
OUTRET=$(echo "$OUT_MSG" | awk '/Parameter Name/ PAR=$4 /Parameter Value/ VAL=$4 /^$/ if (PAR == "O_RETURN") print VAL') 

db2 - 如何在shell中获取存储过程OUT型参数的返回值

create or replace procedure liao.proc1(
  in  I_PARAM1 varchar(10)
 ,in  I_PARAM2 varchar(10)
 ,out O_RETURN integer)
specific liao.proc1
language sql
begin
  set O_RETURN=123;
end
@

OUT_MSG=$(db2 -v "call liao.proc1('$PAR1','$PAR2',?)" )
CMDRET=$?
#注意这里的$OUT_MSG要用双引号括起否则echo出来后换行符会丢失,还有因为是匹配Parameter所以要在英文环境
OUTRET=$(echo "$OUT_MSG" | awk '/Parameter Name/ PAR=$4 /Parameter Value/ VAL=$4 /^$/ if (PAR == "O_RETURN") print VAL')
if [ $CMDRET -ne 0 -o $OUTRET -ne 0 ];
  echo "ERROR..."
fi


参考:

DB2 Stored Procedure Output Value Command Prompt


Question by:
ajexpert
On

Hi experts,

This should be the simple question.

I am calling db2 stored procedure having output parameter from the command prompt.

How to display (ECHO) the output parameter value of the stored procedure at the command prompt?

Thanks

Good Question?  

ocgstylesAccepted Solution on 2007-11-18 at 12:37:43ID: 20308910

Hi ajexpert,

I can give an example, but I'm not sure it will help, since you are working in a Windows environment.  But in case you are familiar with both DOS batch scripting and Unix shell scripting, maybe you can use my shell script as an example to create a DOS batch.

Create a executable file named "get_param_value" that contains:

/usr/bin/awk -v P=$1 '
        /Parameter Name/ PARAM=$4
        /Parameter Value/ VAL=$4
        /^$/ if( PARAM == P ) print VAL '

What this awk script does is look for a parameter that you pass into this script.  When the awk script finds the variable, it finds the value on the next line, then when it encounters the next blank line, it outputs the value if we found the param name.

So, if you use the stored procedure I put in my first post, you can do this:

$ db2 "call test(5,6,?) | ./get_param_value P_OUT

In this case, it would print just the value 11.

Sorry again that this is Unix, but I'm not sure how to do a Windows equivalent.

Keith


FROM: https://www.experts-exchange.com/questions/22966982/DB2-Stored-Procedure-Output-Value-Command-Prompt.html

以上是关于db2 - 如何在shell中获取存储过程OUT型参数的返回值(awk)的主要内容,如果未能解决你的问题,请参考以下文章

DB2存储过程中的OUT参数问题

如何使用 Perl DBI 检索 DB2 SQL 存储过程的返回值?

DB2 LUW - 在存储过程中获取错误行

DB2怎样创建存储过程

DB2 LUW 版本 10.5.0.10 - 在存储过程中获取警告(未找到)行

DB2存储过程