How to return more than one value in PostgreSQL function?

How to return more than one value in PostgreSQL function?

You need to define a new type and define your function to return that type. CREATE OR REPLACE FUNCTION get_object_fields(name text) RETURNS TABLE (f1 varchar(10), f2 varchar(10) /* , */ ) Don’t use CREATE TYPE to return a polymorphic result.

How to return set of records in postgres function?

PL/pgSQL function CREATE OR REPLACE FUNCTION f_foo() — (open_id numeric) — parameter not used RETURNS TABLE (a int, b int, c int) AS $func$ BEGIN RETURN QUERY VALUES (1,2,3) , (3,4,5) , (3,4,5) ; END $func$ LANGUAGE plpgsql IMMUTABLE ROWS 3; In Postgres 9.6 or later you can also add PARALLEL SAFE .

What is a record variable?

A record variable is a composite variable whose internal components, called fields, can have different data types. The value of a record variable and the values of its fields can change. You reference an entire record variable by its name.

What are row type of variable why they are used?

The %ROWTYPE attribute provides a record type that represents a row in a database table. The record can store an entire row of data selected from the table or fetched from a cursor or cursor variable. Variables declared using %ROWTYPE are treated like those declared using a datatype name.

How to return more than one row from PL / pgSQL?

The PL/pgSQL function is a little more complicated, but let’s go through it. The function starts off by declaring a variable r to be of the rowtype holder.

How can I return multiple rows of Records in?

I am trying to return multiple records using RECORD data type, is there a way I can append to RECORD and add/append a new value with each iteration to this RECORD. that is, I want to append to rec so that rec becomes a set of rows when the loop is over, which I can just RETURN at the end of my function. Currently, I am doing this –

Which is the rowtype holder in PL / pgSQL?

The PL/pgSQL function is a little more complicated, but let’s go through it. The function starts off by declaring a variable r to be of the rowtype holder. This variable will be used to store the rows coming from the query in the main body of the function. The main body does a loop over the group by query stated setting r to each row in sequence.

Can a pgSQL function queue up more than one record?

A PL/pgSQL function can also do additional operations on the records or only queue up only some records.