Received: from localhost (unknown [200.46.204.184]) by postgresql.org (Postfix) with ESMTP id AC1C72E009F for ; Wed, 30 Jan 2008 09:32:56 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.184]) (amavisd-maia, port 10024) with ESMTP id 87127-03 for ; Wed, 30 Jan 2008 09:32:44 -0400 (AST) Received: from oxford.xeocode.com (unknown [62.232.55.118]) by postgresql.org (Postfix) with ESMTP id 226A22E00AA for ; Wed, 30 Jan 2008 09:32:52 -0400 (AST) Received: from localhost ([127.0.0.1] helo=oxford.xeocode.com) by oxford.xeocode.com with esmtp (Exim 4.69) (envelope-from ) id 1JKD3V-0001P2-Hi; Wed, 30 Jan 2008 13:32:45 +0000 To: "A. Kretschmer" Cc: Subject: Re: Sql ORDER BY and ASC/DESC question In-Reply-To: <20080130104226.GF26490@a-kretschmer.de> (A. Kretschmer's message of "Wed\, 30 Jan 2008 11\:42\:26 +0100") User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.1 (gnu/linux) X-Draft-From: ("nnimap+mail01.enterprisedb.com:INBOX.sql" 1808) References: <20080130104226.GF26490@a-kretschmer.de> From: Gregory Stark Organization: EnterpriseDB Date: Wed, 30 Jan 2008 13:32:45 +0000 Message-ID: <87tzkvbfr6.fsf@oxford.xeocode.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200801/229 X-Sequence-Number: 30082 "A. Kretschmer" writes: > am Wed, dem 30.01.2008, um 11:35:51 +0100 mailte Jaroslav Sivy folgendes: >> Hello everyone, >> >> I have following problem: am using pl/sql functions to trigger some >> sql code and i need to pass ORDER_BY column name and ASC/DESC sorting >> order as an input parameters into that function and order the result >> based on these input parameters. >> >> The problem is, that the only way is to create query as "string" and >> then execute it as "string". > > Right. >> >> Is there any other way around how to avoid that "string query"? If you're not concerned with the planner being able to find indexes to satisfy these orderings (ie, you don't mind always doing a sort) you could do something like: ORDER BY CASE ? WHEN 1 THEN name ASC WHEN 2 THEN name DESC WHEN 3 THEN height ASC WHEN 4 THEN height DESC ELSE id ASC END But to the planner this will be basically an opaque expression. It won't be able to use any indexes on these columns. Also, incidentally you might want to use text strings instead of integer labels. -- Gregory Stark EnterpriseDB http://www.enterprisedb.com Get trained by Bruce Momjian - ask me about EnterpriseDB's PostgreSQL training!