SQL Server Row Versions
The rowversion
data type is simple to implement, but provides some powerful functionality.
Consider the following, where we create a pair of simple tables.
As we can see, the VerCol
column is populated with sequential data. Note that the value is sequential across the database, not the table
If we update a row
We can see that the VerCol
was automatically updated to the next number in the sequence.
The usefulness is immediateley obvious in two scenarios that come to mind. The first would be a scenario where a user is editing a record that another user is also editing. If User A saves the record, the VerCol
will be updated. When User B goes to save the record, we could check the value of this column against the record User B is working with and prevent the save operation with a warning that the data is out of date.
The second scenario is around batch jobs and/or data exports. If CustomerA and CustomerB occasionally ask for exports of data that have changed since they last asked, we can provide the data and the Max(VerCol)
at the time the export happens. When they next ask for a new set of data, we can simply add criteria stating where VerCol > 0x00000 00000000824
.
Leave a Comment