You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Returns the checksum value computed over a row of a table, or over a list of expressions. CHECKSUM is intended for use in building hash indexes.
36
+
The `CHECKSUM` function returns the checksum value computed over a table row, or over an expression list. Use `CHECKSUM` to build hash indexes.
36
37
37
38
[Transact-SQL Syntax Conventions](../../t-sql/language-elements/transact-sql-syntax-conventions-transact-sql.md)
Specifies that computation is over all the columns of the table. CHECKSUM returns an error if any column is of noncomparable data type. Noncomparable data types are **text**, **ntext**, **image**, XML, and **cursor**, and also **sql_variant** with any one of the preceding types as its base type.
48
+
This argument specifies that the checksum computation covers all table columns. `CHECKSUM` returns an error if any column has a noncomparable data type. Noncomparable data types include:
49
+
50
+
-**cursor**
51
+
-**image**
52
+
-**ntext**
53
+
-**text**
54
+
-**XML**
55
+
56
+
Another noncomparable data type is **sql_variant** with any one of the preceding data types as its base type.
48
57
49
58
*expression*
50
-
Is an [expression](../../t-sql/language-elements/expressions-transact-sql.md) of any type except a noncomparable data type.
59
+
An [expression](../../t-sql/language-elements/expressions-transact-sql.md) of any type, except a noncomparable data type.
51
60
52
61
## Return types
53
62
**int**
54
63
55
64
## Remarks
56
-
CHECKSUM computes a hash value, called the checksum, over its list of arguments. The hash value is intended for use in building hash indexes. If the arguments to CHECKSUM are columns, and an index is built over the computed CHECKSUM value, the result is a hash index. This can be used for equality searches over the columns.
65
+
CHECKSUM computes a hash value, called the checksum, over its argument list. Use this hash value to build hash indexes. A hash index will result if the `CHECKSUM` function has column arguments, and an index is built over the computed CHECKSUM value. This can be used for equality searches over the columns.
57
66
58
-
CHECKSUMsatisfies the properties of a hash function: CHECKSUM applied over any two lists of expressions returns the same value if the corresponding elements of the two lists have the same type and are equal when compared using the equals (=) operator. For this definition, null values of a specified type are considered to compare as equal. 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. 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](../../t-sql/functions/hashbytes-transact-sql.md) 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.
67
+
The `CHECKSUM` function satisfies hash function properties: `CHECKSUM` applied over any two lists of expressions will return the same value, if the corresponding elements of the two lists have the same data type, and if those corresponding elements have equality when compared using the equals (=) operator. Null values of a specified type are defined to compare as equal for `CHECKSUM` function purposes. If at least one of the values in the expression list changes, the list checksum will probably change. However, this is not guaranteed. Therefore, to detect whether values have changed, we recommend use of `CHECKSUM` only if your application can tolerate an occasional missed change. Otherwise, consider using [HashBytes](../../t-sql/functions/hashbytes-transact-sql.md) instead. With a specified MD5 hash algorithm, the probability that HashBytes will return the same result, for two different inputs, is much lower compared to CHECKSUM.
59
68
60
-
The order of expressions affects the resultant value of CHECKSUM. The order of columns used with CHECKSUM(*) is the order of columns specified in the table or view definition. This includes computed columns.
69
+
The expression order affects the computed `CHECKSUM` value. The order of columns used for CHECKSUM(\*) is the order of columns specified in the table or view definition. This includes computed columns.
61
70
62
-
The CHECKSUM value is dependent upon the collation. The same value stored with a different collation will return a different CHECKSUM value.
71
+
The CHECKSUM value depends on the collation. The same value stored with a different collation will return a different CHECKSUM value.
63
72
64
73
## Examples
65
-
The following examples show using `CHECKSUM` to build hash indexes. The hash index is built by adding a computed checksum column to the table being indexed, and then building an index on the checksum column.
74
+
These examples show the use of `CHECKSUM` to build hash indexes.
75
+
76
+
To build the hash index, the first example adds a computed checksum column to the table we want to index. It then builds an index on the checksum column.
66
77
67
78
```sql
68
79
-- Create a checksum index.
@@ -76,7 +87,7 @@ CREATE INDEX Pname_index ON Production.Product (cs_Pname);
76
87
GO
77
88
```
78
89
79
-
The checksum index can be used as a hash index, particularly to improve indexing speed when the column to be indexed is a long character column. The checksum index can be used for equality searches.
90
+
This example shows the use of a checksum index as a hash index. This can help improve indexing speed when the column to index is a long character column. The checksum index can be used for equality searches.
80
91
81
92
```sql
82
93
/*Use the index in a SELECT query. Add a second search
@@ -89,7 +100,7 @@ AND Name = N'Bearing Ball';
89
100
GO
90
101
```
91
102
92
-
Creating the index on the computed column materializes the checksum column, and any changes to the `ProductName` value will be propagated to the checksum column. Alternatively, an index could be built directly on the column indexed. However, if the key values are long, a regular index is not likely to perform as well as a checksum index.
103
+
Index creation on the computed column materializes the checksum column, and any changes to the `ProductName` value will propagate to the checksum column. Alternatively, we could build an index directly on the column we want to index. However, for long key values, a regular index will probably not perform as well as a checksum index.
0 commit comments