[ACCEPTED]-What's the easiest way to return a recordset from a PostgreSQL stored procedure?-plpgsql

Accepted answer
Score: 15

There is also the option of using RETURNS TABLE(...) (as described 5 in the PostgreSQL Manual), which I personally prefer:

CREATE OR REPLACE FUNCTION get_countries()
RETURNS TABLE(
    country_code text,
    country_name text
)
AS $$
    SELECT country_code, country_name FROM country_codes
$$ LANGUAGE sql;

This is effectively 4 the same as using SETOF tablename, but declares the table 3 structure inline instead of referencing 2 an existing object, so joins and such will 1 still work.

Score: 11

You should be able to use output parameters, like 1 this:

CREATE OR REPLACE FUNCTION get_countries(country_code OUT text, country_name OUT text)
RETURNS setof record
AS $$ SELECT country_code, country_name FROM country_codes $$
LANGUAGE sql;

More Related questions