SQL Hash field

Hash fields have many practical uses, most involving capturing data changes. Here’s how to create hash fields.

The built in function BINARY_CHECKSUM() computes the checksum of the passed parameters. The checksum returned by the function is in numeric format. Therefore hash field columns added to table can be an INT datatype.

We have two options for updating our INT field in our table. A computed column or a trigger.

Trigger

Update	[TABLE NAME]
SET		HashField = BINARY_CHECKSUM([YOUR FIELDS HERE])
FROM	[TABLE NAME] t
JOIN	inserted i
ON		i.ID = t.ID

Computed Column

ALTER TABLE [TABLE NAME]

ADD HashField AS BINARY_CHECKSUM(YOUR FIELDS HERE)

Both options result in a working solution. However the trigger approach allows the column to be a simple INT value, this allowing us to include it in indexing. Of course the trigger will result in a greater overhead while inserting, but I think the benefit of an index outweighs the additional overhead.