简体   繁体   English

从DB2存储过程中检索返回值

[英]Retrieve return value from DB2 stored procedure

I have a block of code that is repeated within a DB2 stored procedure. 我有一个在DB2存储过程中重复的代码块。 I would like to separate this out into a new procedure that I can call with parameters and have it return a value. 我想把它分成一个新的过程,我可以用参数调用它并让它返回一个值。

How do I create a procedure to return a value and how do I call this procedure from inside my original procedure? 如何创建一个返回值的过程以及如何从原始过程中调用此过程?

Yes, an output parameter is all it took. 是的,只需输出参数即可。 I couldn't find the right calling syntax in the manual or google though. 我在手册或谷歌中找不到正确的调用语法。

You create the procedure like this: 您创建如下过程:

CREATE PROCEDURE myschema.add(IN a INT, IN b INT, OUT c INT)
BEGIN
    SET c = a + b;
END

And then call it like this (this is what I couldn't figure out): 然后像这样调用它(这是我无法弄清楚的):

DECLARE result INT DEFAULT 0;

CALL myschema.add(10, 20, result);

-- result == 30

And then the output ends up in the supplied result variable. 然后输出结束于提供的result变量中。 You can also have multiple OUT params as well as INOUT params. 您还可以使用多个OUT参数以及INOUT参数。

Sure it seems obvious now. 当然现在看来很明显。 :) :)

How about an output parameter in the proc you call from within your original proc? 在原始proc中调用proc中的输出参数怎么样? Calling a proc is done through the CALL command. 调用proc是通过CALL命令完成的。 It's in the manual ;) 它在手册中;)

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

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