Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"IN" clause limitation in Sql Server

Tags:

sql

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?

like image 217
MSanika Avatar asked Jan 17 '14 05:01

MSanika


People also ask

How do you pass more than 1000 values in clause?

You cannot have more than 1000 literals in an IN clause. You can, however, have SELECT statements in your IN clause which can return an unlimited number of elements i.e. You might try using 'between' clause replacing 'in'... check documentation for correct syntax on using between.

How many values can be passed in in clause in SQL?

In Oracle we can't include more than 1000 values in the “IN” clause.

What is the limit of in clause in Oracle?

In Oracle we can only put up to 1000 values into an IN clause.

What is in in SQL Server?

The SQL Server IN condition will return the records where expression is value1, value2..., or value_n. The SQL Server IN condition is also called the SQL Server IN operator.


2 Answers

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.

like image 173
Heinzi Avatar answered Sep 17 '22 13:09

Heinzi


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.

like image 34
Nick van Esch Avatar answered Sep 18 '22 13:09

Nick van Esch