Hi
I have problem with SQL Server 2000 and queries that contain a large number of conditions. When these queries are sent to the SQL Server I get one of the following error messages depending on the query (error messages edited for readability):
[Microsoft][ODBC SQL Server Driver][SQL Server]
SqlDumpExceptionHandler:
Process 61 generated fatal exception c0000005 EXCEPTION_ACCESS_VIOLATION.
SQL Server is terminating this process. (SQL-HY000)
or
[Microsoft][ODBC SQL Server Driver][SQL Server]
The server encountered a stack overflow during compile time. (SQL-42000)
The limit seems to be about 12500 conditions.
- Have anyone else experienced the same type of problems?
- Are there any settings in SQL Server 2000 that can be tweaked to allow more conditions in a query?
- Does SQL Server 2005 handle more conditions in a query that SQL Server 2000?
--- Edit
The queries in question are typically on the following form
SELECT
...
FROM
TABLE
WHERE
-- the rows I want
( ( COLUMN2 = a AND COLUMN3 = b ) OR ( ( COLUMN2 = c AND COLUMN3 = d ) )
AND
COLUMN1 NOT IN ( ... ) -- The rows that I don't want
The combinations for COLUMN2 and COLUMN3 can be somewhere between 20000 and 60000 I think, though the exact number is not known at this time.
The values for COLUMN2 and COLUMN3 does not follow a pattern, so it is not possible to simplify the query enough so that the problem is avoided altogether.
This values in the IN expression can also reach be about the same number.
--- Edit 2
Solution:
This was the solution I ended up with. As least until any new problems crop up.
- First the query is split into several queries. Together all the queries cover the rows to select. This avoids the condition limit in SQL Server and also seems to greatly improve efficiency in the cases where the limit wasn't reached as well.
- Then result from each query is joined using UNION.
- The total result is then filtered by the application instead of the SQL Server. This seems to be reasonably efficient in this case. If at a later time this proves to be to inefficient, this can probably be replaced by the solution outline in the answer below.