Re: Using SELECT IN with prepared statement - Mailing list pgsql-jdbc

From Ingmar Lötzsch
Subject Re: Using SELECT IN with prepared statement
Date
Msg-id 4919A960.9050904@asci-systemhaus.de
Whole thread Raw
In response to Re: Using SELECT IN with prepared statement  (Alexander Panzhin <jalexoids@gmail.com>)
Responses Re: Using SELECT IN with prepared statement  (Guillaume Cottenceau <gc@mnc.ch>)
List 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.
>>
>


pgsql-jdbc by date:

Previous
From: "Shawn Chasse"
Date:
Subject: Re: Pgsql jdbc driver 8.3 Build 603: Commit deadlock
Next
From: Guillaume Cottenceau
Date:
Subject: Re: Using SELECT IN with prepared statement