CHECKSUM will calculate a hash value for the provided function parameters.
CHECKSUM function when run multiple times for the same parameters, will result in same hash value. But it is not guaranteed that supplying different parameters will always result in a different hash. Therefore using CHECKSUM to uniquely identify each column of a table is not recommended (even with primary keys implemented on the table).
I use this function to see if there were changes to any columns in a given row. Each of my tables have an additional column called ChecksumValue with INT datatype. Periodically (nightly in my case) I calculate Checksum for each row and compare it with the ChecksumValue column and if they turn out to be different, take appropriate action (send out email / restore original value etc).
CREATE TABLE Example(
FName varchar(50),
LName varchar(50),
ChecksumValue int
)
-- Initially:
UPDATE Example SET ChecksumValue = CHECKSUM(FName, LName) -- or CHECKSUM(FName) to track changes to first name only
-- Periodically:
SELECT * FROM Example
WHERE CHECKSUM(Fname, Lname) <> ChecksumValue -- to obtain rows that have changed
If you get more creating in terms of using CHECKSUM function, post a comment and share!
Please consider the environment before printing this blog! Go Green!
Comments