pg.ddx.io pgsql-sql@postgresql.org mailing list archive
help / color / mirror / Atom feedPL/PgSQL - returning multiple columns ...
4+ messages / 3 participants
[nested] [flat]
* PL/PgSQL - returning multiple columns ...
@ 2005-02-02 21:10 Marc G. Fournier <scrappy@postgresql.org>
2005-02-02 22:45 ` Re: PL/PgSQL - returning multiple columns ... George Weaver <gweaver@shaw.ca>
0 siblings, 1 reply; 4+ messages in thread
From: Marc G. Fournier @ 2005-02-02 21:10 UTC (permalink / raw)
To: pgsql-sql
I have a function that I want to return 'server_name, avg(load_avg)' ...
if I wanted to return matching rows in a table, I can do a 'setof
<table>', with a for loop inside ... but what do I set the 'RETURNS' to if
I want to return the results of query that returns only two fields of a
table, or, in the case of the above, one column and oen 'group by' column?
thanks ...
----
Marc G. Fournier Hub.Org Networking Services (http://www.hub.org)
Email: scrappy@hub.org Yahoo!: yscrappy ICQ: 7615664
^ permalink raw reply [nested|flat] 4+ messages in thread
* Re: PL/PgSQL - returning multiple columns ...
2005-02-02 21:10 PL/PgSQL - returning multiple columns ... Marc G. Fournier <scrappy@postgresql.org>
@ 2005-02-02 22:45 ` George Weaver <gweaver@shaw.ca>
2005-02-03 16:48 ` Re: PL/PgSQL - returning multiple columns ... Marc G. Fournier <scrappy@postgresql.org>
0 siblings, 1 reply; 4+ messages in thread
From: George Weaver @ 2005-02-02 22:45 UTC (permalink / raw)
To: Marc G. Fournier <scrappy@postgresql.org>; pgsql-sql
Hi Marc,
One option is to create a simple data type and return the rowtype of the
datatype
eg CREATE TYPE tserverload AS ("server_name" text, "load_avg" int4);
CREATE FUNCTION getserverload()
RETURNS tserverload
AS
'DECLARE
r tserverload%rowtype;
etc.
You would then return r, comprised of r.server_name and r.load_avg.
George
----- Original Message -----
From: "Marc G. Fournier" <scrappy@postgresql.org>
To: <pgsql-sql@postgresql.org>
Sent: Wednesday, February 02, 2005 3:10 PM
Subject: [SQL] PL/PgSQL - returning multiple columns ...
>
> I have a function that I want to return 'server_name, avg(load_avg)' ...
> if I wanted to return matching rows in a table, I can do a 'setof
> <table>', with a for loop inside ... but what do I set the 'RETURNS' to if
> I want to return the results of query that returns only two fields of a
> table, or, in the case of the above, one column and oen 'group by' column?
>
> thanks ...
>
>
> ----
> Marc G. Fournier Hub.Org Networking Services
> (http://www.hub.org)
> Email: scrappy@hub.org Yahoo!: yscrappy ICQ:
> 7615664
>
> ---------------------------(end of broadcast)---------------------------
> TIP 9: the planner will ignore your desire to choose an index scan if your
> joining column's datatypes do not match
>
^ permalink raw reply [nested|flat] 4+ messages in thread
* Re: PL/PgSQL - returning multiple columns ...
2005-02-02 21:10 PL/PgSQL - returning multiple columns ... Marc G. Fournier <scrappy@postgresql.org>
2005-02-02 22:45 ` Re: PL/PgSQL - returning multiple columns ... George Weaver <gweaver@shaw.ca>
@ 2005-02-03 16:48 ` Marc G. Fournier <scrappy@postgresql.org>
2005-02-03 18:15 ` Re: PL/PgSQL - returning multiple columns ... PFC <lists@boutiquenumerique.com>
0 siblings, 1 reply; 4+ messages in thread
From: Marc G. Fournier @ 2005-02-03 16:48 UTC (permalink / raw)
To: George Weaver <gweaver@shaw.ca>; +Cc: Marc G. Fournier <scrappy@postgresql.org>; pgsql-sql
Perfect, worked like a charm ... but the RETURNS still needs to be a
SETOF, other then that, I'm 'away to the races' ... thanks :)
On Wed, 2 Feb 2005, George Weaver wrote:
> Hi Marc,
>
> One option is to create a simple data type and return the rowtype of the
> datatype
>
> eg CREATE TYPE tserverload AS ("server_name" text, "load_avg" int4);
>
> CREATE FUNCTION getserverload()
>
> RETURNS tserverload
>
> AS
>
> 'DECLARE
>
> r tserverload%rowtype;
>
> etc.
>
> You would then return r, comprised of r.server_name and r.load_avg.
>
> George
>
>
>
> ----- Original Message ----- From: "Marc G. Fournier"
> <scrappy@postgresql.org>
> To: <pgsql-sql@postgresql.org>
> Sent: Wednesday, February 02, 2005 3:10 PM
> Subject: [SQL] PL/PgSQL - returning multiple columns ...
>
>
>>
>> I have a function that I want to return 'server_name, avg(load_avg)' ... if
>> I wanted to return matching rows in a table, I can do a 'setof <table>',
>> with a for loop inside ... but what do I set the 'RETURNS' to if I want to
>> return the results of query that returns only two fields of a table, or, in
>> the case of the above, one column and oen 'group by' column?
>>
>> thanks ...
>>
>>
>> ----
>> Marc G. Fournier Hub.Org Networking Services (http://www.hub.org)
>> Email: scrappy@hub.org Yahoo!: yscrappy ICQ: 7615664
>>
>> ---------------------------(end of broadcast)---------------------------
>> TIP 9: the planner will ignore your desire to choose an index scan if your
>> joining column's datatypes do not match
>>
>
>
>
----
Marc G. Fournier Hub.Org Networking Services (http://www.hub.org)
Email: scrappy@hub.org Yahoo!: yscrappy ICQ: 7615664
^ permalink raw reply [nested|flat] 4+ messages in thread
* Re: PL/PgSQL - returning multiple columns ...
2005-02-02 21:10 PL/PgSQL - returning multiple columns ... Marc G. Fournier <scrappy@postgresql.org>
2005-02-02 22:45 ` Re: PL/PgSQL - returning multiple columns ... George Weaver <gweaver@shaw.ca>
2005-02-03 16:48 ` Re: PL/PgSQL - returning multiple columns ... Marc G. Fournier <scrappy@postgresql.org>
@ 2005-02-03 18:15 ` PFC <lists@boutiquenumerique.com>
0 siblings, 0 replies; 4+ messages in thread
From: PFC @ 2005-02-03 18:15 UTC (permalink / raw)
To: Marc G. Fournier <scrappy@postgresql.org>; George Weaver <gweaver@shaw.ca>; +Cc: pgsql-sql
On Thu, 3 Feb 2005 12:48:11 -0400 (AST), Marc G. Fournier
<scrappy@postgresql.org> wrote:
>
> Perfect, worked like a charm ... but the RETURNS still needs to be a
> SETOF, other then that, I'm 'away to the races' ... thanks :)
No SETOF necessary :
CREATE TYPE mytype AS ( number INTEGER, blah TEXT );
CREATE OR REPLACE FUNCTION myfunc( INTEGER ) RETURNS mytype LANGUAGE
plpgsql AS $$ DECLARE _retval mytype; BEGIN _retval.number=$1;
_retval.blah='yeah'; RETURN _retval; END;$$;
SELECT myfunc(22);
myfunc
-----------
(22,yeah)
(1 ligne)
SELECT * FROM myfunc(22);
number | blah
--------+------
22 | yeah
(1 ligne)
^ permalink raw reply [nested|flat] 4+ messages in thread
end of thread, other threads:[~2005-02-03 18:15 UTC | newest]
Thread overview: 4+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2005-02-02 21:10 PL/PgSQL - returning multiple columns ... Marc G. Fournier <scrappy@postgresql.org>
2005-02-02 22:45 ` George Weaver <gweaver@shaw.ca>
2005-02-03 16:48 ` Marc G. Fournier <scrappy@postgresql.org>
2005-02-03 18:15 ` PFC <lists@boutiquenumerique.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