"IN" clause limitation in Sql Server

Does anybody know what is the limit for the number of values one can have in a list of expressions (to test for a match) for the IN clause?


Solution 1:

Yes, there is a limit, but MSDN only specifies that it lies "in the thousands":

Including an extremely large number of values (many thousands) in an IN clause can consume resources and return errors 8623 or 8632. To work around this problem, store the items in the IN list in a table.

Looking at those errors in details, we see that this limit is not specific to IN but applies to query complexity in general:

Error 8623:

The query processor ran out of internal resources and could not produce a query plan. This is a rare event and only expected for extremely complex queries or queries that reference a very large number of tables or partitions. Please simplify the query. If you believe you have received this message in error, contact Customer Support Services for more information.

Error 8632:

Internal error: An expression services limit has been reached. Please look for potentially complex expressions in your query, and try to simplify them.

Solution 2:

It is not specific but is related to the query plan generator exceeding memory limits. I can confirm that with several thousand it often errors but can be resolved by inserting the values into a table first and rephrase the query as

select * from b where z in (select z from c) 

where the values you want in the in clause are in table c. We used this successfully with an in clause of 1-million values.

Solution 3:

Depending on the database engine you are using, there can be limits on the length of an instruction.

SQL Server has a very large limit:

Maximum Capacity Specifications for SQL Server

So, for large IN clauses, it's better to create a temp table, insert the values and do a JOIN. It works faster also.

There is a limit, but you can split your values into separate blocks of in()

Select * 
From table 
Where Col IN (123,123,222,....)
or Col IN (456,878,888,....)

use a table valued parameter in 2008, or some approach described here

Arrays and Lists in SQL Server 2005

Solution 4:

Depending on how you execute the query (JDBC, Hiberante, some kind of SQL-GUI) and when using literal values instead of a sub-query where X in (1, 2, 3...) - you may also encounter the following error:

Too many parameters were provided in this RPC request. The maximum is 2100.

So the limit would be 2100 (or even less when other parameters are present, too) in those cases.