pg.ddx.io pgsql-sql@postgresql.org mailing list archive
help / color / mirror / Atom feedMulti-column returns from pgsql
4+ messages / 3 participants
[nested] [flat]
* Multi-column returns from pgsql
@ 2005-07-22 15:49 Mark R. Dingee <mark.dingee@cox.net>
0 siblings, 1 reply; 4+ messages in thread
From: Mark R. Dingee @ 2005-07-22 15:49 UTC (permalink / raw)
To: pgsql-sql
Hi Everyone,
Does anyone know if/how it's possible to return multi-column sets from a pgsql
function? Right now I'm using something like the following as a work around
CREATE OR REPLACE FUNCTION my_func() returns SETOF TEXT AS '
DECLARE
rec record;
BEGIN
FOR rec IN SELECT txt1, txt2 FROM mytable LOOP
RETURN NEXT rec.txt1;
RETURN NEXT rec.txt2;
END LOOP;
RETURN;
END;' language 'plpgsql';
which leaves me parsing multiple records to achieve the desired end result.
Anyone have any thoughts?
Thanks,
Mark
^ permalink raw reply [nested|flat] 4+ messages in thread
* Re: Multi-column returns from pgsql
@ 2005-07-22 16:56 Jim Buttafuoco <jim@contactbda.com>
parent: Mark R. Dingee <mark.dingee@cox.net>
0 siblings, 1 reply; 4+ messages in thread
From: Jim Buttafuoco @ 2005-07-22 16:56 UTC (permalink / raw)
To: Mark R. Dingee <mark.dingee@cox.net>; pgsql-sql
Mark,
Instead of RETURN NEXT rec.txt1; RETURN NEXT rec.txt2; just use RETURN NEXT rec;
then your select statement would be
select * from my_func() as (txt1 text,txt2 text);
Jim
---------- Original Message -----------
From: "Mark R. Dingee" <mark.dingee@cox.net>
To: pgsql-sql@postgresql.org
Sent: Fri, 22 Jul 2005 11:49:21 -0400
Subject: [SQL] Multi-column returns from pgsql
> Hi Everyone,
>
> Does anyone know if/how it's possible to return multi-column sets from a pgsql
> function? Right now I'm using something like the following as a work around
>
> CREATE OR REPLACE FUNCTION my_func() returns SETOF TEXT AS '
> DECLARE
> rec record;
> BEGIN
> FOR rec IN SELECT txt1, txt2 FROM mytable LOOP
> RETURN NEXT rec.txt1;
> RETURN NEXT rec.txt2;
> END LOOP;
> RETURN;
> END;' language 'plpgsql';
>
> which leaves me parsing multiple records to achieve the desired end result.
>
> Anyone have any thoughts?
>
> Thanks,
> Mark
>
> ---------------------------(end of broadcast)---------------------------
> TIP 9: In versions below 8.0, the planner will ignore your desire to
> choose an index scan if your joining column's datatypes do not
> match
------- End of Original Message -------
^ permalink raw reply [nested|flat] 4+ messages in thread
* Re: Multi-column returns from pgsql
@ 2005-07-22 18:11 Tony Wasson <ajwasson@gmail.com>
parent: Jim Buttafuoco <jim@contactbda.com>
0 siblings, 1 reply; 4+ messages in thread
From: Tony Wasson @ 2005-07-22 18:11 UTC (permalink / raw)
To: jim@contactbda.com; +Cc: Mark R. Dingee <mark.dingee@cox.net>; pgsql-sql
On 7/22/05, Jim Buttafuoco <jim@contactbda.com> wrote:
> Mark,
>
> Instead of RETURN NEXT rec.txt1; RETURN NEXT rec.txt2; just use RETURN NEXT rec;
>
> then your select statement would be
> select * from my_func() as (txt1 text,txt2 text);
>
> Jim
Besides a simple RETURN NEXT, you'll need to return a SETOF some
composite type. You can do something like
CREATE TYPE twotexts_t AS (txt1 TEXT, txt2 TEXT);
CREATE OR REPLACE FUNCTION my_func() returns SETOF twotexts_t AS '
DECLARE
rec record;
BEGIN
FOR rec IN SELECT txt1, txt2 FROM mytable LOOP
RETURN NEXT
END LOOP;
RETURN;
END;' language 'plpgsql';
^ permalink raw reply [nested|flat] 4+ messages in thread
* Re: Multi-column returns from pgsql
@ 2005-07-23 12:57 Jim Buttafuoco <jim@contactbda.com>
parent: Tony Wasson <ajwasson@gmail.com>
0 siblings, 0 replies; 4+ messages in thread
From: Jim Buttafuoco @ 2005-07-23 12:57 UTC (permalink / raw)
To: Tony Wasson <ajwasson@gmail.com>; +Cc: Mark R. Dingee <mark.dingee@cox.net>; pgsql-sql
or just return setof RECORD (version 7.4 +)
---------- Original Message -----------
From: Tony Wasson <ajwasson@gmail.com>
To: jim@contactbda.com
Cc: "Mark R. Dingee" <mark.dingee@cox.net>, pgsql-sql@postgresql.org
Sent: Fri, 22 Jul 2005 11:11:09 -0700
Subject: Re: [SQL] Multi-column returns from pgsql
> On 7/22/05, Jim Buttafuoco <jim@contactbda.com> wrote:
> > Mark,
> >
> > Instead of RETURN NEXT rec.txt1; RETURN NEXT rec.txt2; just use RETURN NEXT rec;
> >
> > then your select statement would be
> > select * from my_func() as (txt1 text,txt2 text);
> >
> > Jim
>
> Besides a simple RETURN NEXT, you'll need to return a SETOF some
> composite type. You can do something like
>
> CREATE TYPE twotexts_t AS (txt1 TEXT, txt2 TEXT);
>
> CREATE OR REPLACE FUNCTION my_func() returns SETOF twotexts_t AS '
> DECLARE
> rec record;
> BEGIN
> FOR rec IN SELECT txt1, txt2 FROM mytable LOOP
> RETURN NEXT
> END LOOP;
> RETURN;
> END;' language 'plpgsql';
>
> ---------------------------(end of broadcast)---------------------------
> TIP 6: explain analyze is your friend
------- End of Original Message -------
^ permalink raw reply [nested|flat] 4+ messages in thread
end of thread, other threads:[~2005-07-23 12:57 UTC | newest]
Thread overview: 4+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2005-07-22 15:49 Multi-column returns from pgsql Mark R. Dingee <mark.dingee@cox.net>
2005-07-22 16:56 ` Jim Buttafuoco <jim@contactbda.com>
2005-07-22 18:11 ` Tony Wasson <ajwasson@gmail.com>
2005-07-23 12:57 ` Jim Buttafuoco <jim@contactbda.com>
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