Is there any limit for SQL server IN clause can filter?

658    Asked by AnneBell in SQL Server , Asked on Jul 19, 2021

Is there any limit for the content that SQL server IN clause filter can handle? For example: SELECT Name FROM People WHERE Job IN (All the values goes here) Microsoft docs for IN says: "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." but is there any exact or approximate number for many thousands of values


Answered by Celina Lagunas

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. 

The solution is to just stop worrying about it and use a table-valued parameter. If you have the values in C# in such a way that you can build a comma-separated list as a string to concatenate together in a query, you have the values in C# in such a way that you could stuff them in a DataTable or other structure (maybe that's where they're coming from in the first place), and pass that structure as a parameter to the stored procedure.



Your Answer

Interviews

Parent Categories