CHECKSUM_AGG

Transact-SQL Reference

Transact-SQL Reference

CHECKSUM_AGG

Returns the checksum of the values in a group. Null values are ignored.

Syntax

CHECKSUM_AGG ( [ ALL | DISTINCT ] expression )

Arguments

ALL

Applies the aggregate function to all values. ALL is the default.

DISTINCT

Specifies that CHECKSUM_AGG return the checksum of unique values.

expression

Is a constant, column, or function, and any combination of arithmetic, bitwise, and string operators. expression is an expression of the int data type. Aggregate functions and subqueries are not allowed.

Return Types

Returns the checksum of all expression values as int.

Remarks

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.

If one of the values in the expression list changes, the checksum of the list also usually changes. However, there is a small chance that the checksum will not change.

CHECKSUM_AGG has similar functionality with other aggregate functions. For more information, see Aggregate Functions.

Examples
A. Use CHECKSUM_AGG with BINARY_CHECKSUM to detect changes in a table.

This example uses CHECKSUM_AGG with the BINARY_CHECKSUM function to detect changes in the Products table.

USE Northwind
GO
SELECT   CHECKSUM_AGG(BINARY_CHECKSUM(*))
FROM   Products 
B. Use CHECKSUM_AGG with BINARY_CHECKSUM to detect changes in a column of a table.

This example detects changes in UnitsInStock column of the Products table in the Northwind database.

--Get the checksum value before the column value is changed.
USE   Northwind
GO
SELECT   CHECKSUM_AGG(CAST(UnitsInStock AS int))
FROM   Products

Here is the result set:

57

--Change the value of a row in the column
UPDATE   Products   --
SET   UnitsInStock=135
WHERE   UnitsInStock=125

--Get the checksum of the modified column.
SELECT   CHECKSUM_AGG(CAST(UnitsInStock AS int))
FROM   Products

Here is the result set:

195

See Also

BINARY_CHECKSUM

CHECKSUM