Hi, all!
We have received some users questions about how to send a list of values to be used in queries using the "in" operator. Something like:
select foo, bar from table where foo in (blah1, blah2, blah3);
Npgsql supports array-like parameter values and the first idea to have this working would try to use it directly:
NpgsqlCommand command = new NpgsqlCommand("select * from tablee where field_serial in (:parameterlist)", conn);
ArrayList l = new ArrayList();
l.Add(5);
l.Add(6);
command.Parameters.Add(new NpgsqlParameter("parameterlist", NpgsqlDbType.Array | NpgsqlDbType.Integer));
command.Parameters[0].Value = l.ToArray();
NpgsqlDataReader dr = command.ExecuteReader();
but unfortunately this won't work as expected. Npgsql will send a query like this:
select * from tablee where field_serial in ((array[5,6])::int4[])
And Postgresql will complain with the following message:
ERROR: operator does not exist: integer = integer[]
This is because it is trying to compare the integer value of the column with an array of integers.
But, thanks to Josh Cooley and Jerónimo Milea who posted a solution on our forum, you just have to change your "in" operator to use the "any" operator and it will work as expected:
NpgsqlCommand command = new NpgsqlCommand("select * from tablee where field_serial = any (:parameterlist)", conn);
So if you want to use a query to compare a list of values, remember to use the any operator.
We have received some users questions about how to send a list of values to be used in queries using the "in" operator. Something like:
select foo, bar from table where foo in (blah1, blah2, blah3);
Npgsql supports array-like parameter values and the first idea to have this working would try to use it directly:
NpgsqlCommand command = new NpgsqlCommand("select * from tablee where field_serial in (:parameterlist)", conn);
ArrayList l = new ArrayList();
l.Add(5);
l.Add(6);
command.Parameters.Add(new NpgsqlParameter("parameterlist", NpgsqlDbType.Array | NpgsqlDbType.Integer));
command.Parameters[0].Value = l.ToArray();
NpgsqlDataReader dr = command.ExecuteReader();
but unfortunately this won't work as expected. Npgsql will send a query like this:
select * from tablee where field_serial in ((array[5,6])::int4[])
And Postgresql will complain with the following message:
ERROR: operator does not exist: integer = integer[]
This is because it is trying to compare the integer value of the column with an array of integers.
But, thanks to Josh Cooley and Jerónimo Milea who posted a solution on our forum, you just have to change your "in" operator to use the "any" operator and it will work as expected:
NpgsqlCommand command = new NpgsqlCommand("select * from tablee where field_serial = any (:parameterlist)", conn);
So if you want to use a query to compare a list of values, remember to use the any operator.
Comments
select * from field in the(collection);
but postgre rocks anyway :) Loving it :)
what I mean is I can't seem to recall if other rdbms have array facility. there ain't seems no way on other rdbms to allow passing of variable list in a straightforward manner. it's only Postgres I can think that has that sort of neatness. and with the newer Npgsql support for arrays, app development becomes way, way much better.
see other rdbms's kludge solutions:
http://forums.whirlpool.net.au/forum-replies-archive.cfm/928435.html
http://www.sqlmag.com/Article/ArticleID/44939/sql_server_44939.html
http://knol.google.com/k/franck-pachot/oracle-passing-a-list-as-bind-variable/17uabcrki6uux/2#
And thank you for your support and feedback. You rock!
Thanks.
Please come visit my site leadership styles
when you got time. Thanks.
when you got time. Thanks.
Please come visit my site symptoms of lupus when you got time.
Thanks.
Thanks.
cosmetic dentists edinburgh
undermount sinks