1,两个函数的Syntax相同,返回值的Data Type 都是 int类型。
CHECKSUM ( * | expression [ ,...n ] ) BINARY_CHECKSUM ( * | expression [ ,...n ] )
CHECKSUM is intended for use in building hash indexes.
BINARY_CHECKSUM can be used to detect changes to a row of a table.
2,受collation和 column order的影响
BINARY_CHECKSUM and CHECKSUM are similar functions:
The CHECKSUM and BINARY_CHECKSUM value is dependent upon the collation. The same value stored with a different collation will return a different CHECKSUM value.
They can be used to compute a checksum value on a list of expressions, and the order of expressions affects the resultant value. The order of expressions affects the resultant value of CHECKSUM and BINARY_CHECKSUM. The order of columns used with CHECKSUM(*) is the order of columns specified in the table or view definition. This includes computed columns.
CHECKSUM and BINARY_CHECKSUM return different values for the string data types, where locale can cause strings with different representation to compare equal. The string data types are char, varchar, nchar, nvarchar, or sql_variant (if the base type of sql_variant is a string data type).
For example, the BINARY_CHECKSUM values for the strings "McCavity" and "Mccavity" are different. In contrast, in a case-insensitive server, CHECKSUM returns the same checksum values for those strings. CHECKSUM values should not be compared with BINARY_CHECKSUM values.
3,精确度
Checksum 和 binary_checksum 都有很小的几率出现hash conflict,即在计算不同数据的值时,返回相同的hash值。HashBytes函数是SQL Server提供的精确度最高的hash 函数。出现has conflict的几率更低。
If one of the values in the expression list changes, the checksum of the list also generally changes. However, there is a small chance that the checksum will not change.
BINARY_CHECKSUM(*), computed on any row of a table, returns the same value as long the row is not subsequently modified. BINARY_CHECKSUM(*) will return a different value for most, but not all, changes to the row, and can be used to detect most row modifications.
For this reason, we do not recommend using CHECKSUM to detect whether values have changed, unless your application can tolerate occasionally missing a change. Consider using HashBytes instead. When an MD5 hash algorithm is specified, the probability of HashBytes returning the same result for two different inputs is much lower than that of CHECKSUM.
4, Example: create hash index using checksum
SET ARITHABORT ON; USE AdventureWorks2012; GO ALTER TABLE Production.Product ADD cs_Pname AS CHECKSUM(Name); GO CREATE INDEX Pname_index ON Production.Product (cs_Pname); GO SELECT * FROM Production.Product WHERE CHECKSUM(N'Bearing Ball') = cs_Pname AND Name = N'Bearing Ball'; go
参考文档:
https://msdn.microsoft.com/en-us/library/ms173784(v=sql.110).aspx
https://msdn.microsoft.com/en-us/library/ms189788(v=sql.110).aspx