SQL 从 DB2 存储过程中检索返回值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/373386/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Retrieve return value from DB2 stored procedure
提问by molasses
I have a block of code that is repeated within a DB2 stored procedure. I would like to separate this out into a new procedure that I can call with parameters and have it return a value.
我有一个在 DB2 存储过程中重复的代码块。我想把它分离成一个新的过程,我可以用参数调用它并让它返回一个值。
How do I create a procedure to return a value and how do I call this procedure from inside my original procedure?
如何创建返回值的过程以及如何从原始过程中调用此过程?
采纳答案by Frans Bouma
How about an output parameter in the proc you call from within your original proc? Calling a proc is done through the CALL command. It's in the manual ;)
您从原始 proc 中调用的 proc 中的输出参数如何?调用 proc 是通过 CALL 命令完成的。它在手册中;)
回答by molasses
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.
You can also have multiple OUT
params as well as INOUT
params.
然后输出以提供的result
变量结束。您还可以有多个OUT
参数以及INOUT
参数。
Sure it seems obvious now. :)
当然现在看起来很明显。:)