From scrappy@postgresql.org Wed Feb 2 21:11:09 2005 X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 7B0988B9B48 for ; Wed, 2 Feb 2005 21:11:09 +0000 (GMT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 43933-07 for ; Wed, 2 Feb 2005 21:10:58 +0000 (GMT) Received: from ganymede.hub.org (blk-224-186-245.eastlink.ca [24.224.186.245]) by svr1.postgresql.org (Postfix) with ESMTP id 8982D8B9D56 for ; Wed, 2 Feb 2005 21:10:58 +0000 (GMT) Received: by ganymede.hub.org (Postfix, from userid 1000) id 1D9E2372FB; Wed, 2 Feb 2005 17:10:59 -0400 (AST) Received: from localhost (localhost [127.0.0.1]) by ganymede.hub.org (Postfix) with ESMTP id 1CD53372F7 for ; Wed, 2 Feb 2005 17:10:59 -0400 (AST) Date: Wed, 2 Feb 2005 17:10:59 -0400 (AST) From: "Marc G. Fournier" X-X-Sender: scrappy@ganymede.hub.org To: pgsql-sql@postgresql.org Subject: PL/PgSQL - returning multiple columns ... Message-ID: <20050202170643.R27579@ganymede.hub.org> MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII; format=flowed X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=1.094 tagged_above=0 required=5 tests=AWL, FORGED_RCVD_HELO, RCVD_IN_NJABL_DUL, RCVD_IN_SORBS_DUL X-Spam-Level: * X-Archive-Number: 200502/48 X-Sequence-Number: 20159 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 ', 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 From gweaver@shaw.ca Wed Feb 2 22:45:37 2005 X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 667638B9E42; Wed, 2 Feb 2005 22:45:37 +0000 (GMT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 49868-05; Wed, 2 Feb 2005 22:45:30 +0000 (GMT) Received: from pd6mo4no.prod.shaw.ca (idcmail-mo2no.cg.shawcable.net [64.59.134.9]) by svr1.postgresql.org (Postfix) with ESMTP id E8EB18B9B48; Wed, 2 Feb 2005 22:45:32 +0000 (GMT) Received: from pd8mr7no.prod.shaw.ca (pd8mr7no-qfe2.prod.shaw.ca [10.0.144.228]) by l-daemon (Sun ONE Messaging Server 6.0 HotFix 1.01 (built Mar 15 2004)) with ESMTP id <0IBB000TZ37RNI40@l-daemon>; Wed, 02 Feb 2005 15:45:27 -0700 (MST) Received: from pn7ml4no.prod.shaw.ca ([10.0.149.113]) by pd8mr7no.prod.shaw.ca (Sun ONE Messaging Server 6.0 HotFix 1.01 (built Mar 15 2004)) with ESMTP id <0IBB00AIK37RLQP0@pd8mr7no.prod.shaw.ca>; Wed, 02 Feb 2005 15:45:27 -0700 (MST) Received: from Dell4500 (S01060004236fc1f7.wp.shawcable.net [24.77.13.13]) by l-daemon (iPlanet Messaging Server 5.2 HotFix 1.18 (built Jul 28 2003)) with SMTP id <0IBB0014T37K4B@l-daemon>; Wed, 02 Feb 2005 15:45:27 -0700 (MST) Date: Wed, 02 Feb 2005 16:45:42 -0600 From: George Weaver Subject: Re: PL/PgSQL - returning multiple columns ... To: "Marc G. Fournier" , pgsql-sql@postgresql.org Message-id: <00c701c50978$f01c9e20$6400a8c0@Dell4500> MIME-version: 1.0 X-MIMEOLE: Produced By Microsoft MimeOLE V6.00.2900.2180 X-Mailer: Microsoft Outlook Express 6.00.2900.2180 Content-type: text/plain; reply-type=response; charset=iso-8859-1; format=flowed Content-transfer-encoding: 7bit X-Priority: 3 X-MSMail-priority: Normal References: <20050202170643.R27579@ganymede.hub.org> X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=1.568 tagged_above=0 required=5 tests=AWL, DNS_FROM_RFC_ABUSE, DNS_FROM_RFC_POST, FORGED_RCVD_HELO X-Spam-Level: * X-Archive-Number: 200502/50 X-Sequence-Number: 20161 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" To: 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 >
', 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 > From scrappy@postgresql.org Thu Feb 3 16:48:21 2005 X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id F06428B9F61; Thu, 3 Feb 2005 16:48:21 +0000 (GMT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 50060-08; Thu, 3 Feb 2005 16:48:12 +0000 (GMT) Received: from ganymede.hub.org (blk-224-186-245.eastlink.ca [24.224.186.245]) by svr1.postgresql.org (Postfix) with ESMTP id 038BD8B9F72; Thu, 3 Feb 2005 16:48:12 +0000 (GMT) Received: by ganymede.hub.org (Postfix, from userid 1000) id 8C5B038E68; Thu, 3 Feb 2005 12:48:11 -0400 (AST) Received: from localhost (localhost [127.0.0.1]) by ganymede.hub.org (Postfix) with ESMTP id 8AEA638CF7; Thu, 3 Feb 2005 12:48:11 -0400 (AST) Date: Thu, 3 Feb 2005 12:48:11 -0400 (AST) From: "Marc G. Fournier" X-X-Sender: scrappy@ganymede.hub.org To: George Weaver Cc: "Marc G. Fournier" , pgsql-sql@postgresql.org Subject: Re: PL/PgSQL - returning multiple columns ... In-Reply-To: <00c701c50978$f01c9e20$6400a8c0@Dell4500> Message-ID: <20050203124751.C62562@ganymede.hub.org> References: <20050202170643.R27579@ganymede.hub.org> <00c701c50978$f01c9e20$6400a8c0@Dell4500> MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII; format=flowed X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=1.098 tagged_above=0 required=5 tests=AWL, FORGED_RCVD_HELO, RCVD_IN_NJABL_DUL, RCVD_IN_SORBS_DUL X-Spam-Level: * X-Archive-Number: 200502/68 X-Sequence-Number: 20179 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" > > To: > 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
', >> 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 From lists@boutiquenumerique.com Thu Feb 3 18:12:25 2005 X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id D09208B9F8D for ; Thu, 3 Feb 2005 18:12:25 +0000 (GMT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 63588-03 for ; Thu, 3 Feb 2005 18:12:20 +0000 (GMT) Received: from boutiquenumerique.com (boutiquenumerique.com [82.67.9.10]) by svr1.postgresql.org (Postfix) with ESMTP id 226508B9F21 for ; Thu, 3 Feb 2005 18:12:19 +0000 (GMT) Received: (qmail 13590 invoked from network); 3 Feb 2005 19:12:41 +0100 Received: from unknown (HELO musicbox) (boutiquenumerique-lists@192.168.0.2) by boutiquenumerique.com with SMTP; 3 Feb 2005 19:12:41 +0100 To: "Marc G. Fournier" , "George Weaver" Cc: pgsql-sql@postgresql.org Subject: Re: PL/PgSQL - returning multiple columns ... References: <20050202170643.R27579@ganymede.hub.org> <00c701c50978$f01c9e20$6400a8c0@Dell4500> <20050203124751.C62562@ganymede.hub.org> Message-ID: Date: Thu, 03 Feb 2005 19:15:04 +0100 From: PFC Organization: =?iso-8859-15?Q?La_Boutique_Num=E9rique?= Content-Type: text/plain; format=flowed; delsp=yes; charset=iso-8859-15 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit In-Reply-To: <20050203124751.C62562@ganymede.hub.org> User-Agent: Opera M2/7.54 (Linux, build 751) X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.107 tagged_above=0 required=5 tests=AWL, UPPERCASE_25_50 X-Spam-Level: X-Archive-Number: 200502/70 X-Sequence-Number: 20181 On Thu, 3 Feb 2005 12:48:11 -0400 (AST), Marc G. Fournier 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)