"IN" clause limitation in Sql Server

SqlSql Server

Sql Problem Overview


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?

Sql Solutions


Solution 1 - Sql

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

> Explicitly including an extremely large number of values (many thousands of values separated by commas) within the parentheses, 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, and use a SELECT subquery within an IN clause.

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 - Sql

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 - Sql

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 - Sql

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMSanikaView Question on Stackoverflow
Solution 1 - SqlHeinziView Answer on Stackoverflow
Solution 2 - SqlNick van EschView Answer on Stackoverflow
Solution 3 - SqlAmarnath BalasubramanianView Answer on Stackoverflow
Solution 4 - SqlicyerasorView Answer on Stackoverflow