X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (av.hub.org [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 931CED84FB for ; Wed, 2 Nov 2005 10:42:54 -0400 (AST) 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 74149-07 for ; Wed, 2 Nov 2005 14:42:50 +0000 (GMT) Received: from mail.mits.se (unknown [194.236.40.10]) by svr1.postgresql.org (Postfix) with ESMTP id B1866D830C for ; Wed, 2 Nov 2005 10:42:51 -0400 (AST) Received: from robertsdator ([192.168.0.27]) by mail.mits.se (mail.mits.se) with ESMTP id FZS03152 for ; Wed, 2 Nov 2005 15:42:52 +0100 From: "Robert Blixt" To: Subject: Function with dynamic command (EXECUTE) not working Date: Wed, 2 Nov 2005 15:40:50 +0100 MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit X-Mailer: Microsoft Office Outlook, Build 11.0.6353 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2180 Thread-index: AcXfu2uXZGE6sF5BSfey3CPEu4wgKg== Message-Id: <20051102144251.B1866D830C@svr1.postgresql.org> X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, score=0.927 required=5 tests=[MSGID_FROM_MTA_ID=0.927, UPPERCASE_25_50=0] X-Spam-Score: 0.927 X-Spam-Level: X-Archive-Number: 200511/28 X-Sequence-Number: 23280 Hello, I am trying to create a function that will allow me to dynamically choose the ORDER BY sequence. I also want the result of the SELECT statement to be returned. Far as I can tell this can not be done with EXECUTE alone rather I should use FOR .. IN EXECUTE. The result of the statement is a single column of type varchar. The result amount can be 0 - n. However, I can not get it to work This is pretty much how far I have come.. [CODE] CREATE OR REPLACE FUNCTION "public"."testfunction"( lisnotactive boolean, lorderby1 varchar, lorderby2 varchar ) RETURNS SETOF record AS $BODY$ DECLARE rRec RECORD; BEGIN FOR rRec IN EXECUTE( 'SELECT DISTINCT stationplace.name FROM stationplace, employee WHERE employee.isnotactive = ' || lisnotactive || 'ORDER BY ' || quote_ident(lorderby1) || ', ' || quote_ident(lorderby2) ) LOOP END LOOP; RETURN NEXT rRec; END; $BODY$ LANGUAGE 'plpgsql' VOLATILE; [/CODE] Any help is highly appreciated. Kind Regards, Robert