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

本文示例了如何在shell中通过db2命令调用存储过程并获取OUT类型参数的返回值,使用awk处理输出以提取特定参数值。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

示例:

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

 

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值