Technical Article

Use HASHBYTES to identify changes

,

If you need to check a row for changes you can use HASHBYTES to do so.  

Here is how to create a HASH value for the entire row.  You want to use SHA1 to make sure there are (virtually) no collisions.

For this case the hash column should be VARBINARY(20)

Make sure <primary key> is the key for that table. If there are multiple rows with that key, the hash will include them all.

The perfect use for this would be a nightly data load where you get the entire universe of data each night, maybe thousands of rows, but only a small percentage of the rows actually change.  This allows you to identify the deltas and just process them instead of having to process everything.

You could also use this to identify all row changes for a given table by comparing a saved hash value to the current hash value.  Just remember, the hash is static so if you need to update it you will have to create a process to do so.

/*For this case the hash column should be VARBINARY(20)*/
UPDATE <table name>
SET rowDiffHash = HASHBYTES('SHA1',(select * from <table name> where <primary key> = a.<primary key> for xml path)) 
FROM  <table name> a

/*Make sure <primary key> is the key for that table. If there are multiple rows with that key, the hash will include them all.*/

Rate

2.2 (5)

You rated this post out of 5. Change rating

Share

Share

Rate

2.2 (5)

You rated this post out of 5. Change rating