Re: ORDER BY in prepared statements

From: Paul Jungwirth <pj(at)illuminatedcomputing(dot)com>
To: Bryn Jeffries <bryn(dot)jeffries(at)sydney(dot)edu(dot)au>
Cc: "pgsql-general(at)postgresql(dot)org" <pgsql-general(at)postgresql(dot)org>
Subject: Re: ORDER BY in prepared statements
Date: 2015-01-21 21:06:49
Message-ID: CA+6hpamMqQnhJSjv0zPk=N5FR9gDN4+uVyg8Gv=dvhJSdNX-Gw@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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.

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Adrian Klaver 2015-01-21 21:18:14 Re: ORDER BY in prepared statements
Previous Message Bryn Jeffries 2015-01-21 20:51:23 ORDER BY in prepared statements