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 515CF329E0E for ; Mon, 18 Oct 2004 17:25:24 +0100 (BST) 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 35241-06 for ; Mon, 18 Oct 2004 16:25:15 +0000 (GMT) Received: from megazone.bigpanda.com (megazone.bigpanda.com [64.147.171.210]) by svr1.postgresql.org (Postfix) with ESMTP id 6356E32A3E9 for ; Mon, 18 Oct 2004 17:25:13 +0100 (BST) Received: by megazone.bigpanda.com (Postfix, from userid 1001) id 22CDB35591; Mon, 18 Oct 2004 09:25:11 -0700 (PDT) Received: from localhost (localhost [127.0.0.1]) by megazone.bigpanda.com (Postfix) with ESMTP id 214C2354C4; Mon, 18 Oct 2004 09:25:11 -0700 (PDT) Date: Mon, 18 Oct 2004 09:25:11 -0700 (PDT) From: Stephan Szabo To: Kent Anderson Cc: "Pgsql-Sql@Postgresql. Org" Subject: Re: Ordering a record returned from a stored procedure In-Reply-To: Message-ID: <20041018092408.N14426@megazone.bigpanda.com> References: MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.0 tagged_above=0.0 required=5.0 tests= X-Spam-Level: X-Archive-Number: 200410/113 X-Sequence-Number: 19216 On Mon, 18 Oct 2004, Kent Anderson wrote: > I am pulling a report from the database using a stored procedure but cannot > get the information to return in a specific order unless I hardcode the > order by clause. > > CREATE OR REPLACE FUNCTION submissionreport(INTEGER, DATE, TEXT) RETURNS > setof submissionrec AS ' > DECLARE > result submissionrec%rowtype; > hmhmkey ALIAS for $1; > submissiondate ALIAS for $2; > sort ALIAS for $3; > > BEGIN > RAISE NOTICE ''The sort order should be: %.'', sort; > FOR result IN > SELECT > (..... select all necessary fields ...) > FROM > (.... tables ...) > WHERE > (... contraints) > > ORDER BY sort > LOOP > RETURN next result; > END LOOP; > > RETURN result; > > > END; > ' LANGUAGE plpgsql; > > What am I missing? The returned data is ordered if the "Order By" clause has > the values hard coded but doesn't seem to read the "sort" variable. You're telling it to order by the value of the third argument, not the value of the column with the name of the third argument. I think right now you'd need to use EXECUTE to put it in as if it were the expression to sort on.