Received: from localhost (postgresql.org [64.49.215.8]) by postgresql.org (Postfix) with ESMTP id 05E31475F1C for ; Fri, 24 Jan 2003 20:42:37 -0500 (EST) Received: from bach.ccinet.ab.ca (bach.ccinet.ab.ca [198.161.96.1]) by postgresql.org (Postfix) with ESMTP id 525F3475F1A for ; Fri, 24 Jan 2003 20:42:36 -0500 (EST) Received: from incentre.net (squid.incentre.net [206.75.213.195]) by bach.ccinet.ab.ca (8.12.6/8.12.6) with ESMTP id h0P1lM7f046831 for ; Fri, 24 Jan 2003 18:47:23 -0700 (MST) (envelope-from guy@incentre.net) Message-ID: <3E31EB8F.6040606@incentre.net> Date: Fri, 24 Jan 2003 18:42:39 -0700 From: Guy Fraser Organization: J I Reasearch User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.0.1) Gecko/20021003 X-Accept-Language: en MIME-Version: 1.0 To: pgsql-sql@postgresql.org Subject: Re: Getting multiple rows in plpgsql function References: <42509.216.86.192.34.1043437147.squirrel@www.la-rubber.com> <20030124223626.GB24536@cc.usu.edu> Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: by AMaViS new-20020517 X-Archive-Number: 200301/409 X-Sequence-Number: 11274 NOTE: This is a feature in 7.3 it was either added or fixed, so you will not be able to do this unless you are using version 7.3. Remember to backup with pg_dumpall before you upgrade. This is a sample sent to me earlier this week, that iterates an integer array: ########Cut Here######## CREATE TYPE group_view AS (grosysid int4, groname name, usesysid int4, usename name); CREATE OR REPLACE FUNCTION expand_groups() RETURNS SETOF group_view AS ' DECLARE rec record; groview record; low int; high int; BEGIN FOR rec IN SELECT grosysid FROM pg_group LOOP SELECT INTO low replace(split_part(array_dims(grolist),'':'',1),''['','''')::int FROM pg_group WHERE grosysid = rec.grosysid; IF low IS NULL THEN low := 1; high := 1; ELSE SELECT INTO high replace(split_part(array_dims(grolist),'':'',2),'']'','''')::int FROM pg_group WHERE grosysid = rec.grosysid; IF high IS NULL THEN high := 1; END IF; END IF; FOR i IN low..high LOOP SELECT INTO groview g.grosysid, g.groname, s.usesysid, s.usename FROM pg_shadow s join pg_group g on s.usesysid = g.grolist[i] WHERE grosysid = rec.grosysid; RETURN NEXT groview; END LOOP; END LOOP; RETURN; END; ' LANGUAGE 'plpgsql' WITH ( iscachable, isstrict ); CREATE VIEW groupview AS SELECT * FROM expand_groups(); ########Cut Here######## One of the tricks is that you apparently need to use the CREATE TYPE commands to define the returned result. The veiw at the end just makes queries look like a table is being queried rather than a function. I hope this helps. Roberto Mello wrote: > On Fri, Jan 24, 2003 at 11:39:07AM -0800, David Durst wrote: > >>I am wondering how you would handle a select that returns multiple rows >>in a plpgsql function? >> >>In other words lets say I wanted to iterate through the results in >>the function. > > > There are examples in the PL/pgSQL documentation that show you how to do it. > > -Roberto >