postgresql_16:6.1.5.5 SQL Procedures with Output Parameters
PostgreSQL 16 > Server Programming > Extending SQL > Query Language (SQL) Functions > SQL Procedures with Output Parameters
Output parameters are also supported in procedures, but they work a bit differently from functions. In CALL commands, output parameters must be included in the argument list. For example, the bank account debiting routine from earlier could be written like this: CREATE PROCEDURE tp1 (accountno integer, debit numeric, OUT new_balance numeric) AS $$ UPDATE bank SET balance = balance - debit WHERE accountno = tp1.accountno RETURNING balance; $$ LANGUAGE SQL; To call this procedure, an argument matching the OUT parameter must be included. It's customary to write NULL: CALL tp1(17, 100.0, NULL); If you write something else, it must be an expression that is implicitly coercible to the declared type of the parameter, just as for input parameters. Note however that such an expression will not be evaluated.
When calling a procedure from PL/pgSQL, instead of writing NULL you must write a variable that will receive the procedure's output. See (see postgresql_16:6.6.6.3) for details.