天天看点

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代替;

由数据库自动赋值,在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数据类型

二,使用RowVersion的示例

1,查看数据库RowVersion的当前值

RowVersion数据类型

2,创建含有RowVersion列的Table,并插入数据

<a></a>

3,查看当前的RowVersion和表中的数据

RowVersion数据类型

4,更新表,查看RowVersion值的变化

RowVersion数据类型

 5,在事务rollback时,查看RowVersion值的变化

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,如需转载请自行联系原作者