Search code examples
sql-serverconditional-statementschecksum

CHECKSUM_AGG and WHERE conditions in SQL


I am trying to find out when a table has been changed but only for rows that meet a specific condition. I have tried with this query:

SELECT CHECKSUM_AGG(BINARY_CHECKSUM(LastChanged)) FROM Products WHERE Category = 2 WITH (NOLOCK)

and I get a General SQL Server error. If I execute the query directly, the error is:

Incorect syntax near '('

Is it possible to get the checksum for the rows that meet the condition only?


Solution

  • The error is being caused by an incorrectly placed query hint, not by your use of a CHECKSUM expression.

    Though it might help to read up on whether you should really be using NOLOCK at all, try putting WITH (NOLOCK) after your FROM:

    SELECT CHECKSUM_AGG(BINARY_CHECKSUM(LastChanged)) 
    FROM Products WITH (NOLOCK)
    WHERE Category = 2