X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (neptune.hub.org [200.46.204.2]) by svr1.postgresql.org (Postfix) with ESMTP id 7C6EBD1B480 for ; Thu, 27 Nov 2003 08:42:58 +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 47479-08 for ; Thu, 27 Nov 2003 04:42:28 -0400 (AST) Received: from fw-berlin.bund.de (fw-berlin.bund.de [194.95.177.102]) by svr1.postgresql.org (Postfix) with ESMTP id 4D14ED1B43F for ; Thu, 27 Nov 2003 04:42:25 -0400 (AST) Received: by fw-berlin.bund.de (8.11.6p2G/8.11.6) id hAR8gNQ11715 for pgsql-sql@postgresql.org; Thu, 27 Nov 2003 09:42:23 +0100 (CET) Received: (from localhost) by a2.fw.bln.ivbb.bund.de (MSCAN) id 3/a2.fw.bln.ivbb.bund.de/smtp-gw/mscan; Thu Nov 27 09:42:23 2003 Date: Thu, 27 Nov 2003 09:42:09 +0100 (CET) From: Andreas Tille X-X-Sender: tillea@wr-linux02.rki.ivbb.bund.de Cc: PostgreSQL SQL Subject: Re: Scaler forms as function arguments In-Reply-To: <200311261725.21975.dev@archonet.com> Message-Id: References: <200311261725.21975.dev@archonet.com> MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII X-OriginalArrivalTime: 27 Nov 2003 08:34:36.0453 (UTC) FILETIME=[4A499550:01C3B4C1] X-Virus-Scanned: by amavisd-new at postgresql.org X-Archive-Number: 200311/302 X-Sequence-Number: 16117 On Wed, 26 Nov 2003, Richard Huxton wrote: > Not as you've done it. You could pass in text "(1,2,3)", build your query and > use EXECUTE to execute it. This boils down the question to the problem which occured with your promissing link below, because I need to use PL/pgSQL, right? > Alternatively, you might be able to do it with an > array parameter (sorry, I don't use arrays, so I can't be sure). I'll give that a try. > Read the section on plpgsql in the manuals, you return results one at a time. > For some examples, see http://techdocs.postgresql.org/ and look for the "Set > Returning Functions" item. A very interesting article but if I try the example code: create table department(id int primary key, name text); create table employee(id int primary key, name text, salary int, departmentid int references department); insert into department values (1, 'Management'); insert into department values (2, 'IT'); insert into employee values (1, 'John Smith', 30000, 1); insert into employee values (2, 'Jane Doe', 50000, 1); insert into employee values (3, 'Jack Jackson', 60000, 2); create function GetEmployees() returns setof employee as 'select * from employee;' language 'sql'; create type holder as (departmentid int, totalsalary int8); create function SqlDepartmentSalaries() returns setof holder as ' select departmentid, sum(salary) as totalsalary from GetEmployees() group by departmentid ' language 'sql'; create or replace function PLpgSQLDepartmentSalaries() returns setof holder as ' declare r holder%rowtype; begin for r in select departmentid, sum(salary) as totalsalary from GetEmployees() group by departmentid loop return next r; end loop; return; end ' language 'plpgsql'; I get: test=# select PLpgSQLDepartmentSalaries() ; WARNING: Error occurred while executing PL/pgSQL function plpgsqldepartmentsalaries WARNING: line 5 at return next ERROR: Set-valued function called in context that cannot accept a set test=# Any hint what might be wrong here? I'm using PostgreSQL 7.3.2 under Debian GNU/Linux (testing). Kind regards Andreas.