I was reading this on PostgreSQL Tutorials:
In case you want to return a value from a stored procedure, you can use output parameters. The final values of the output parameters will be returned to the caller.
And then I found a difference between function and stored procedure at DZone:
Stored procedures do not return a value, but stored functions return a single value
Can anyone please help me resolve this.
If we can return anything from stored procedures, please also let me know how to do that from a SELECT
statement inside the body.
If I am wrong somewhere please inform.
Best Answer
Both your sources are plain wrong.
A
FUNCTION
basically always returns something.void
at the minimum, a single value, a row, or a set of rows ("set-returning function", a.k.a. "table-function") - the only variant that can return nothing, i.e. no row. Called from within plpgsql code, you have to actively dismiss results if you don't want them. See:A
PROCEDURE
(Postgres 11 or later) returns a single row if it has any arguments with theINOUT
mode. zsheep already provided an example.Consider a
DO
statement to run ad-hoc plpgsql code without passing or returning anything.To address your core question:
The same way as in functions, as that's in the realm of general PL/pgSQL: assign to the parameter with the
INTO
keyword:db<>fiddle here
Related: