Re: Using SELECT IN with prepared statement

From: Ingmar Lötzsch <iloetzsch(at)asci-systemhaus(dot)de>
To: pgsql-jdbc(at)postgresql(dot)org
Subject: Re: Using SELECT IN with prepared statement
Date: 2008-11-11 15:48:48
Message-ID: 4919A960.9050904@asci-systemhaus.de
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-jdbc

Hello Alexander,

> You need to write: select * from users where userid = ANY ?

I have not found another way to use an array. You can execute the
following statements:

SELECT 1 = ANY (ARRAY[1, 2]);
SELECT 1 = ANY ('{3, 2}');
SELECT 1 = ANY ('{}');
SELECT 1 IN (1, 2);

You can't execute

SELECT 1 IN (ARRAY[1, 2]);

because IN expects "a parenthesized list of scalar expressions".

See
http://www.postgresql.org/docs/8.3/interactive/functions-comparisons.html
too.

Of course you can concatenate the values like

String idlist = "1, 2";
String sql = "SELECT" + ... + "WHERE id IN (" + idlist + ")";

and execute the statement. But there is no parameter.

Ingmar

Alexander Panzhin schrieb:
> Hi,
>
> You need to write: select * from users where userid = ANY ?
>
> See
> http://www.postgresql.org/docs/8.3/interactive/functions-comparisons.html
>
> Tiago Alves wrote:
>> Greetings!
>>
>> Sorry if you answer this before, I couldn't find it.
>>
>> I want to do something like:
>>
>> PreparedStatement ps = connection.prepareStatement("select * from
>> users where userid in ?");
>> ps.setSomething(anIntArray);
>>
>> Is this possible? How?
>>
>> Thanks.
>> Tiago A.
>>
>

In response to

Responses

Browse pgsql-jdbc by date

  From Date Subject
Next Message Guillaume Cottenceau 2008-11-11 16:16:56 Re: Using SELECT IN with prepared statement
Previous Message Shawn Chasse 2008-11-11 14:05:23 Re: Pgsql jdbc driver 8.3 Build 603: Commit deadlock