Returning from a function with OUT parameter

It would work like this:

CREATE OR REPLACE FUNCTION name_function(param_1 varchar
                                   , OUT param_2 bigint)
  LANGUAGE plpgsql AS
$func$
BEGIN
    INSERT INTO table (collumn_seq, param_1)  -- "param_1" also the column name?
    VALUES (DEFAULT, param_1)
    RETURNING collumn_seq
    INTO param2;
END
$func$;

Normally, you would add a RETURN statement, but with OUT parameters, this is optional.
Refer to the manual for more details:

The simple case can be covered with a plain SQL function.
And you can omit the target column that shall get its DEFAULT value.
And you can just as well use a RETURNS clause in this case:

CREATE OR REPLACE FUNCTION name_function(param_1 varchar)
  RETURNS bigint
  LANGUAGE sql AS
$func$
INSERT INTO table (param_1)  -- "param_1" also the column name?
VALUES (param_1)
RETURNING collumn_seq;
$func$;

Leave a Comment