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代替;
由資料庫自動指派,在Insert或Update指令中,不能顯式指派;
在 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.

二,使用RowVersion的示例
1,檢視資料庫RowVersion的目前值
2,建立含有RowVersion列的Table,并插入資料
<a></a>
3,檢視目前的RowVersion和表中的資料
4,更新表,檢視RowVersion值的變化
5,在事務rollback時,檢視RowVersion值的變化
當事務復原時,RowVersion字段的值不會復原,但資料庫的RowVersion字段的值會遞增,即資料庫計數器(Counter)不在事務内,其值隻會遞增,不會復原。
參考文檔:
<a href="https://msdn.microsoft.com/en-us/library/ms182776(v=sql.110).aspx" target="_blank">RowVersion (Transact-SQL)</a>
本文版權歸作者和部落格園所有,歡迎轉載,但未經作者同意,必須保留此段聲明,且在文章頁面醒目位置顯示原文連接配接,否則保留追究法律責任的權利。
本文轉自悅光陰部落格園部落格,原文連結:http://www.cnblogs.com/ljhdo/p/4791838.html,如需轉載請自行聯系原作者