Re: Error with returning SETOF Record

From: Joe Conway <mail(at)joeconway(dot)com>
To: A E <cooljoint(at)yahoo(dot)com>
Cc: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, pgsql-hackers(at)postgresql(dot)org
Subject: Re: Error with returning SETOF Record
Date: 2003-12-27 23:24:18
Message-ID: 3FEE14A2.2060309@joeconway.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

A E wrote:
> I guess the question I have then is how do I return data from a
> select statement using the record datatype? This is only a test
> function I was trying to get info from, but my application is based
> on passing the table name to the function dynamically. How do I do
> this? The documentation is sketchy when it comes to the record
> datatype.

You need to specify in your query the column definition that will
actually be returned. Here's a simple example:

create table t1 (f1 int, f2 text);
insert into t1 values (1,'a');
insert into t1 values (2,'b');
create table t2 (f1 int, f2 float8);
insert into t2 values (3, 3.14);
insert into t2 values (4, 2.8);

create or replace function getrec(text) returns setof record as '
DECLARE
ft record;
begin
FOR ft IN EXECUTE ''SELECT * FROM '' || $1 LOOP
RETURN NEXT ft;
END LOOP;
return;
end;
' LANGUAGE 'plpgsql' VOLATILE;

regression=# SELECT * FROM getrec('t1') AS (f1 int, f2 text);
f1 | f2
----+----
1 | a
2 | b
(2 rows)

regression=# SELECT * FROM getrec('t2') AS (f1 int, f2 float8);
f1 | f2
----+------
3 | 3.14
4 | 2.8
(2 rows)

HTH,

Joe

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Gaetano Mendola 2003-12-28 01:02:43 Little mess in RPM RH ?
Previous Message Tom Lane 2003-12-27 23:16:54 Re: [GENERAL] Strange permission problem regarding pg_settings