X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.2]) by svr1.postgresql.org (Postfix) with ESMTP id 0000AD1BA9F for ; Thu, 26 Feb 2004 15:29:36 +0000 (GMT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (neptune.hub.org [200.46.204.2]) (amavisd-new, port 10024) with ESMTP id 68392-06 for ; Thu, 26 Feb 2004 11:29:33 -0400 (AST) Received: from megazone.bigpanda.com (megazone.bigpanda.com [64.147.171.210]) by svr1.postgresql.org (Postfix) with ESMTP id 9422FD1B97B for ; Thu, 26 Feb 2004 11:29:29 -0400 (AST) Received: by megazone.bigpanda.com (Postfix, from userid 1001) id D9CB4357E6; Thu, 26 Feb 2004 07:29:27 -0800 (PST) Received: from localhost (localhost [127.0.0.1]) by megazone.bigpanda.com (Postfix) with ESMTP id D849A3570E; Thu, 26 Feb 2004 07:29:27 -0800 (PST) Date: Thu, 26 Feb 2004 07:29:27 -0800 (PST) From: Stephan Szabo To: Kumar Cc: psql Subject: Re: Return more than a record In-Reply-To: <036901c3fc6f$a9159840$7502a8c0@hdsc.com> Message-ID: <20040226071613.Y54753@megazone.bigpanda.com> References: <036901c3fc6f$a9159840$7502a8c0@hdsc.com> MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII X-Virus-Scanned: by amavisd-new at postgresql.org X-Archive-Number: 200402/367 X-Sequence-Number: 17057 On Thu, 26 Feb 2004, Kumar wrote: > Get the following from the groups > create or replace function ExpensiveDepartments() returns setof table1 as Note that at least the example with this name in the SetReturningFunctions guide seems to use setof int as the return type. > ' > declare > r table1%rowtype; > begin > for r in select departmentid, sum(salary) as totalsalary > from GetEmployees() group by departmentid loop > > if (r.totalsalary > 70000) then > r.totalsalary := CAST(r.totalsalary * 1.75 as int8); > else > r.totalsalary := CAST(r.totalsalary * 1.5 as int8); > end if; > > if (r.totalsalary > 100000) then > return next r.departmentid; > end if; > > end loop; > return; > end > ' > language 'plpgsql'; > Is possible for me to return a variable along with that 'return' statement? Because the table 'table1' contains some date > column. I have done some calculation on those columns and want to return the calculated date along with that row of the > table1. How to do that. Please shed some light. If you want to return a composite type, you can make another rowtype variable that has the set of columns (and their types) to return, fill in the values to return and then do return next with that variable. For example, to say return departmentid, sum(salary) and the computed "totalsalary" from the above, you might do something like (untested so there might be syntactic errors) create type holder as (departmentid int, totalsalary int8); create type holder2 as (departmentid int, sumsalary int8, totalsalary int8); create or replace function ExpensiveDepartments() returns setof holder2 as ' declare r holder%rowtype; s holder2%rowtype; begin for r in select departmentid, sum(salary) as totalsalary from GetEmployees() group by departmentid loop s.departmentid := r.departmentid; s.sumsalary := r.totalsalary; if (r.totalsalary > 70000) then s.totalsalary := CAST(r.totalsalary * 1.75 as int8); else s.totalsalary := CAST(r.totalsalary * 1.5 as int8); end if; if (s.totalsalary > 100000) then return next s; end if; end loop; return; end ' language 'plpgsql'; The important differences here are that we've got a new rowtype variable s of the return type and that we fill s with the values from r (the select) plus the calculation that we're doing (rather than before where we just overwrote the values in r.totalsalary) and then we return next s rather than a particular field.