CHECKSUM_AGG function is used to get checksum of values in a group. It
ignores null values in computation.
CHECKSUM_AGG
can be used along with BINARY_CHECKSUM to detect changes in a table.
The order
of the rows in the table does not affect the result of CHECKSUM_AGG. In
addition, CHECKSUM_AGG functions may be used with the DISTINCT keyword and the
GROUP BY clause.
Syntax of CHECKSUM function :
CHECKSUM ( * | expression [ ,...n ] )
* means
that the computation is over all the columns of the table.
Expression is any valid sql expression of any type.
Example of CHECKSUM function :
Example 1 : Use of CHECKSUM_AGG function in SELECT clause to get checksum
in group
SELECT CHECKSUM_AGG(CONVERT(INT,UnitsInStock))
FROM Products
Output
50
Now update some of the rows and again run the query.
UPDATE Products
SET UnitsInStock = 15
WHERE UnitsInStock <
10
SELECT CHECKSUM_AGG(CONVERT(INT,UnitsInStock))
FROM Products
Output
61
Above example displays checksum of unit price field in the products
table.
Example 2 : Using CHECKSUM_AGG function with BINARY_CHECKSUM to detect changes in a table
SELECT CHECKSUM_AGG(BINARY_CHECKSUM(UnitsInStock))
FROM Products
Output
7913472
Above
example displays checksum to detect changes in the unit in stock field of
product table.