CREATE OR REPLACE PROCEDURE test1(OUT i int)
LANGUAGE plpgsql AS
$$
BEGIN
i:=10;
end;
$$;
CALL test1(1); --Works. Return 10. Passed value is ignored.
CALL test(); --Doesn't work. ERROR: procedure test1() does not exist Hint: No procedure matches the given name and argument types. You might need to add explicit type casts. Position: 6
The passed argument won’t be assigned as initial value if it is OUT
. I know both OUT
and INOUT
parameter of the procedure must correspond to a variable in the CALL
statement. But why does it implemented this way?. Is there any specific reason?.
2
Answers
In SQL procedures, the OUT parameters are meant to capture the values generated by the procedure and return them to the calling environment. Unlike IN parameters, which are used to pass values into the procedure, OUT parameters are used to pass values out of the procedure.
When calling a procedure with OUT parameters, you need to provide variables to capture the output values. You cannot have an empty list of parameters for a procedure call with OUT parameters because there would be no way to capture the output values.
For example, if you have a procedure with an OUT parameter like this:
You would call it like this:
Here, result is a variable that captures the value returned by the OUT parameter of the procedure. The OUT parameter cannot be empty in the procedure call because there needs to be a way to capture the output value.
The reason why you need to pass a parameter is because there is one in the procedure definition. PostgreSQL has overloading of functions and procedures, and the argument allows PostgreSQL to determine if it should invoke a procedure
test1
that has no parameters, one that has aninteger
parameter or one that has avarchar
parameter.You can find that in the documentation: