pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Sean Davis <sdavis2@mail.nih.gov>
To: Christian Paul B. Cosinas <cpc@cybees.com>
To: pgsql-sql@postgresql.org
Subject: Re: SETOF RECORD RETURN VALUE
Date: Wed, 26 Oct 2005 06:58:16 -0400
Message-ID: <BF84D988.11C6E%sdavis2@mail.nih.gov> (raw)
In-Reply-To: <000001c5da18$e1d9ba20$1e21100a@ghwk02002147>

On 10/26/05 6:34 AM, "Christian Paul B. Cosinas" <cpc@cybees.com> wrote:

> Hi I am having some problem with function that returns SETOF RECORD
> 
> Here is my function:
> 
> CREATE OR REPLACE FUNCTION test_record(text)
> RETURNS SETOF RECORD AS
> $BODY$
> 
> 
> DECLARE
> p_table_name ALIAS FOR $1;
> temp_rec RECORD;
> v_query text;
> 
> BEGIN 
> 
> v_query = 'SELECT * FROM ' || p_table_name; FOR temp_rec IN EXECUTE v_query
> LOOP
> RETURN NEXT temp_rec;
> END LOOP;
> 
> RETURN ;
> 
> END;
> 
> $BODY$
> LANGUAGE 'plpgsql' VOLATILE;
> 
> 
> And here is how I execute the function:
> select * from test_record('field_list')
> 
> I have this error:
> 
> ERROR:  a column definition list is required for functions returning
> "record"

Since Postgres doesn't know what to expect from your function, you have to
tell it by giving the list of columns that are actually returned:

select * from test_record('field_list') as s(a,b,c,d)

where a,b,c,d are the columns in your returned set.  (ie., in your example,
if p_table_name has 5 columns, you would use "as s(a,b,c,d,e)", etc.).

See here for more detail:

http://techdocs.postgresql.org/guides/SetReturningFunctions

Sean




view thread (2+ messages)

Message-ID: <BF84D988.11C6E%sdavis2@mail.nih.gov>
Permalink:  ../BF84D988.11C6E%25sdavis2@mail.nih.gov/
Also on:    postgresql.org/message-id/BF84D988.11C6E%sdavis2@mail.nih.gov

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: sdavis2@mail.nih.gov, cpc@cybees.com
  Subject: Re: SETOF RECORD RETURN VALUE
  In-Reply-To: <BF84D988.11C6E%sdavis2@mail.nih.gov>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox