Re: ORDER BY in prepared statements - Mailing list pgsql-general

From Paul Jungwirth
Subject Re: ORDER BY in prepared statements
Date
Msg-id CA+6hpamMqQnhJSjv0zPk=N5FR9gDN4+uVyg8Gv=dvhJSdNX-Gw@mail.gmail.com
Whole thread Raw
In response to ORDER BY in prepared statements  (Bryn Jeffries <bryn.jeffries@sydney.edu.au>)
Responses Re: ORDER BY in prepared statements  (David G Johnston <david.g.johnston@gmail.com>)
List pgsql-general
> In a number of places on the web I've seen it claimed that ordering can be
> set via prepared statements.
> ...
> sandbox=# PREPARE testplan(text) AS
> SELECT * FROM test ORDER BY $1;
>
> But the output is not what one would expect:
>
> sandbox=# EXECUTE testplan('gender');
> ...
> As opposed to:
> sandbox=# SELECT * FROM test ORDER BY gender;

Your prepared statement version is actually comparable to this SQL:

    SELECT * FROM test ORDER BY 'gender'

which is effectually ordering by random.

I'm not sure how to make a prepared statement that lets you name a
column when you execute it. Maybe someone else can chime in if that's
possible.

Paul


--
_________________________________
Pulchritudo splendor veritatis.


pgsql-general by date:

Previous
From: Bryn Jeffries
Date:
Subject: ORDER BY in prepared statements
Next
From: Adrian Klaver
Date:
Subject: Re: ORDER BY in prepared statements