Thread: 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
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 >
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
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)