UPDATE statement processing records that should be eliminated by WHERE clause

All we need is an easy explanation of the problem, so here it is.

I am getting a very odd error. Consider the table below:

    Key1 INT , Key2 INT ,
    x SMALLINT , y INT , z INT ,

-- insert many records


For some reason, the following update statement tries to divide by zero. This can only happen if c=0 or c=1. The WHERE clause clearly specifies c>1.

-- this fails with divide-by-zero error
UPDATE  #MyTable
        WHEN a - SQUARE ( b ) / c <= 0 THEN 0
        ELSE ( a - SQUARE ( b ) / c ) / ( c - 1 )
WHERE   x <= 622 AND c > 1 AND ( y > 0 OR z > 0 )

The problem is completely eliminated if I redundantly check for c<=1 in my CASE expression:

-- this completes without an error
UPDATE  #MyTable
        WHEN ( c <= 1 ) OR ( a - SQUARE ( b ) / c <= 0 ) THEN 0
        ELSE ( a - SQUARE ( b ) / c ) / ( c - 1 )
WHERE   x <= 622 AND c > 1 AND ( y > 0 OR z > 0 )

Has anyone encountered this before? Why would SQL Server touch the records with c>1?

The problem is also avoided if there is no index on the table (the index is useful in steps later in the procedure). Why would the existence of an index cause a condition in the WHERE clause to be ignored?

How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 1

You should not make any assumptions about how SQL Server will process your query, except this: you should always assume that SQL Server can process your query in a way that is different from how it is explicitly written on the screen. And also this behavior can change based on any of the factors that can influence whether a new plan will be used for the next execution of even the same query, so if you apply a hint or change the query in any way or add or remove an index and the error goes away, don’t assume the error won’t come back tomorrow.

In this case, SQL Server is processing a calculation before it is eliminating rows from the WHERE clause. The way you avoid this is, like you said, ensuring that those rows are also filtered out inside the CASE expression (not statement).

A more common but similar approach is this kind of thing:

SELECT DATEPART(MONTH, varchar_column)
FROM dbo.some_table
WHERE ISDATE(varchar_column) = 1;

In many scenarios you will get an error message because SQL Server tried to apply the date functions against some values in the column that didn’t turn out to be dates (and this attempt occurred prior to filtering). The workaround is tedious – use the CASE expression – but necessary unless you have some other way to verify the worthiness of the column (e.g. a computed column or fixing the data type in the first place). Just keep in mind that even this can fail to "short circuit" in some scenarios.

SELECT CASE WHEN ISDATE(varchar_column) = 1 
  THEN DATEPART(MONTH, varchar_column) END
FROM dbo.some_table
WHERE ISDATE(varchar_column) = 1;

This is explained more thoroughly in the following feedback item by Erland Sommarskog:

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply