Too many ANDs or ORs in expression (limit %d per expression level). Try splitting query or limiting ANDs and ORs.
This error occurs when you use more than 250 AND or OR expressions in a query, or when you use more than 250 values in a where clause. For example, the following example WHERE clause(s) would result in an error:
select * from tblTest where strDataCol_1 = '1' and strDataCol_2 = '2' and strDataCol_n = 'n' and . . . strDataCol_251 = '251' select * from tblTest where strDataCol in ('1', '2', ..., '251')
You must restructure the query so that the limit is not exceeded. For example, you could restructure the second example above to perform a subquery or join operation, as shown here:
select * FROM tblTest WHERE strDataCol in (SELECT strDataCol FROM tblTest Values) SELECT tt.* FROM tblTest tt, tblTestVals ttv WHERE tt.strDataCol = ttv.strDataCol SELECT * FROM tblTest WHERE strDataCol between 1 and 251
Where tblTestValues holds all the necessary values for the IN clause.
Be very careful as to how you restructure the query. Some operations can perform better than others. The first restructuring example could cause a subquery to be performed; the second restructuring example could cause the Cartesian product to be used to derive the results. For more information on query optimization, see the Microsoft SQL Server Database Developer's Companion.