I'm using the following code:
SELECT * FROM table
WHERE Col IN (123,123,222,....)
However, if I put more than ~3000 numbers in the IN
clause, SQL throws an error.
Does anyone know if there's a size limit or anything similar?!!
I'm using the following code:
SELECT * FROM table
WHERE Col IN (123,123,222,....)
However, if I put more than ~3000 numbers in the IN
clause, SQL throws an error.
Does anyone know if there's a size limit or anything similar?!!
You can use tuples like this: SELECT * FROM table WHERE (Col, 1) IN ((123,1),(123,1),(222,1),....)
There are no restrictions on number of these. It compares pairs.
Depending on your version, use a table valued parameter in 2008, or some approach described here:
Arrays and Lists in SQL Server 2005
Parameterize the query and pass the ids in using a Table Valued Parameter.
For example, define the following type:
Along with the following stored procedure:
There is a limit, but you can split your values into separate blocks of in()
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:
http://msdn.microsoft.com/en-us/library/ms143432.aspx
ORACLE has a very easy to reach limit on the other side.
So, for large IN clauses, it's better to create a temp table, insert the values and do a JOIN. It works faster also.
Why not do a where IN a sub-select...
Pre-query into a temp table or something...
then...