"A. Kretschmer" <andreas.kretschmer@schollglas.com> 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 ASCWHEN 2 THEN name DESCWHEN 3 THEN height ASCWHEN 4 THEN height DESCELSE id ASCEND
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'sPostgreSQL training!