RowVersion數據類型是系統自動生成的,唯一的,二進位數字,數值和binary(8)相同,RowVersion通常用作給Table的數據行加版本戳,存儲大小為 8 個位元組。RowVersion數據類型只是永恆遞增的數字,不保留日期或時間,但是可以使用RowVersion來比較數據行更新時間的先 ...
RowVersion數據類型是系統自動生成的,唯一的,二進位數字,數值和binary(8)相同,RowVersion通常用作給Table的數據行加版本戳,存儲大小為 8 個位元組。RowVersion數據類型只是永恆遞增的數字,不保留日期或時間,但是可以使用RowVersion來比較數據行更新時間的先後,如果@rv1<@rv2,那麼表明@rv2的更新發生在@rv1之後。
每個資料庫都只有一個自增的計數器(Counter),每次對擁有RowVersion 欄位的Table執行Insert或Update命令,該計數器都會增加,該計數器是Database RowVersion。一個Table最多有一個RowVersion 欄位,只要對Table執行Insert或Update命令,該欄位就會被更新為計數器(Counter)的最新值。
Each database has a counter that is incremented for each insert or update operation that is performed on a table that contains a rowversion column within the database. This counter is the database rowversion. This tracks a relative time within a database, not an actual time that can be associated with a clock. A table can have only one rowversion column. Every time that a row with a rowversion column is modified or inserted, the incremented database rowversion value is inserted in the rowversion column.
RowVersion欄位的特性:
- 由於每個資料庫只有一個Counter,因此,RowVersion的值在當前資料庫中是唯一的,所有擁有RowVersion欄位的Table,該欄位的值都是不同的;
- 資料庫的RowVersion 只會遞增,不會回滾;如果更新表數據(Insert或Update)的事務回滾,該Table的RowVersion欄位的值會回滾,但是資料庫的RowVersion不會回滾;
- TimeStamp 已過時,避免用於產品設計中,使用RowVersion代替;
在 Table 中增加RowVersion欄位,能夠檢查該行是否被更新(insert或update),如果當前值跟最近一次記錄的RowVersion值不同,說明該數據行被更新過。
You can use the rowversion column of a row to easily determine whether any value in the row has changed since the last time it was read. If any change is made to the row, the rowversion value is updated. If no change is made to the row, the rowversion value is the same as when it was previously read.
一,查看當前資料庫的RowVersion
1,全局變數@@DBTS用於返回當前資料庫的RowVersion,@@DBTS 返回值的數據類型是varbinary(8)。
@@DBTS value is not rolled back when a transaction rolls back or when an INSERT or UPDATE query causes an error.
@@DBTS returns the value of the current rowversion data type for the database. The rowversion is guaranteed to be unique in the database. @@DBTS returns a varbinary which is the last-used rowversion value of the current database. A new rowversion value is generated when a row with a rowversion column is inserted or updated. Any INSERT, UPDATE and CREATE queries will internally increment the rowversion values.
2,非確定性函數 MIN_ACTIVE_ROWVERSION() 用於返回當前資料庫的下一個RowVersion值,其值是@@DBTS+1。
MIN_ACTIVE_ROWVERSION is a non-deterministic function that returns the lowest active rowversion value in the current database. A new rowversion value is typically generated when an insert or update is performed on a table that contains a column of typerowversion. If there are no active values in the database, MIN_ACTIVE_ROWVERSION returns the same value as @@DBTS + 1.
select @@DBTS as dbts,MIN_ACTIVE_ROWVERSION() as min_active
二,使用RowVersion的示例
1,查看資料庫RowVersion的當前值
declare @rv rowversion set @rv=@@DBTS select @rv as rv
2,創建含有RowVersion列的Table,並插入數據
create table dbo.dt_rv ( id int not null, rv rowversion not null ) insert into dbo.dt_rv(id) values(1)
3,查看當前的RowVersion和表中的數據
select id,rv,@@dbts as dbts from dbo.dt_rv
4,更新表,查看RowVersion值的變化
update dbo.dt_rv set id=2 where id=1 select id,rv,@@dbts as dbts from dbo.dt_rv
5,在事務rollback時,查看RowVersion值的變化
begin tran insert into dbo.dt_rv(id) values(1) rollback tran
select id,rv,@@dbts as dbts from dbo.dt_rv
當事務回滾時,RowVersion欄位的值不會回滾,但資料庫的RowVersion欄位的值會遞增,即資料庫計數器(Counter)不再事務內,其值只會遞增,不會回滾。
參考文檔: