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.
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. You can also have multiple OUT
params as well as INOUT
params.
Sure it seems obvious now. :)
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 ;)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.