Click to Rate and Give Feedback
TechNet
TechNet Library
SQL Server
SQL Server 2008
Database Engine
Technical Reference
 rowversion (Transact-SQL)

  Switch on low bandwidth view
Community Content
In this section
Statistics Annotations (2)
Collapse All/Expand All Collapse All
Other versions are also available for the following:
SQL Server 2008 Books Online (October 2009)
rowversion (Transact-SQL)

Is a data type that exposes automatically generated, unique binary numbers within a database. rowversion is generally used as a mechanism for version-stamping table rows. The storage size is 8 bytes. The rowversion data type is just an incrementing number and does not preserve a date or a time. To record a date or time, use a datetime2 data type.

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. This property makes a rowversion column a poor candidate for keys, especially primary keys. Any update made to the row changes the rowversion value and, therefore, changes the key value. If the column is in a primary key, the old key value is no longer valid, and foreign keys referencing the old value are no longer valid. If the table is referenced in a dynamic cursor, all updates change the position of the rows in the cursor. If the column is in an index key, all updates to the data row also generate updates of the index.

timestamp is the synonym for the rowversion data type and is subject to the behavior of data type synonyms. In DDL statements, use rowversion instead of timestamp wherever possible. For more information, see Data Type Synonyms (Transact-SQL).

The Transact-SQL timestamp data type is different from the timestamp data type defined in the ISO standard.

ms182776.note(en-us,SQL.100).gifNote:
The timestamp syntax is deprecated. This feature will be removed in a future version of Microsoft SQL Server. Avoid using this feature in new development work, and plan to modify applications that currently use this feature.

In a CREATE TABLE or ALTER TABLE statement, you do not have to specify a column name for the timestamp data type, for example:

CREATE TABLE ExampleTable (PriKey int PRIMARY KEY, timestamp);

If you do not specify a column name, the SQL Server Database Engine generates the timestamp column name; however, the rowversion synonym does not follow this behavior. When you use rowversion, you must specify a column name, for example:

CREATE TABLE ExampleTable2 (PriKey int PRIMARY KEY, VerCol rowversion) ;
ms182776.note(en-us,SQL.100).gifNote:
Duplicate rowversion values can be generated by using the SELECT INTO statement in which a rowversion column is in the SELECT list. We do not recommend using rowversion in this manner.

A nonnullable rowversion column is semantically equivalent to a binary(8) column. A nullable rowversion column is semantically equivalent to a varbinary(8) column.

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. To return the current rowversion value for a database, use @@DBTS.

You can add a rowversion column to a table to help maintain the integrity of the database when multiple users are updating rows at the same time. You may also want to know how many rows and which rows were updated without re-querying the table.

For example, assume that you create a table named MyTest. You populate some data in the table by running the following Transact-SQL statements.

CREATE TABLE MyTest (myKey int PRIMARY KEY
    ,myValue int, RV rowversion);
GO 
INSERT INTO MyTest (myKey, myValue) VALUES (1, 0);
GO 
INSERT INTO MyTest (myKey, myValue) VALUES (2, 0);
GO

You can then use the following sample Transact-SQL statements to implement optimistic concurrency control on the MyTest table during the update.

DECLARE @t TABLE (myKey int);
UPDATE MyTest
SET myValue = 2
    OUTPUT inserted.myKey INTO @t(myKey) 
WHERE myKey = 1 
    AND RV = myValue;
IF (SELECT COUNT(*) FROM @t) = 0
    BEGIN
        RAISERROR ('error changing row with myKey = %d'
            ,16 -- Severity.
            ,1 -- State 
            ,1) -- myKey that was changed 
    END;

myValue is the rowversion column value for the row that indicates the last time that you read the row. This value must be replaced by the actual rowversion value. An example of the actual rowversion value is 0x00000000000007D3.

You can also put the sample Transact-SQL statements into a transaction. By querying the @t variable in the scope of the transaction, you can retrieve the updated myKey column of the table without requerying the MyTest table.

The following is the same example using the timestamp syntax:

CREATE TABLE MyTest2 (myKey int PRIMARY KEY
    ,myValue int, TS timestamp);
GO 
INSERT INTO MyTest2 (myKey, myValue) VALUES (1, 0);
GO 
INSERT INTO MyTest2 (myKey, myValue) VALUES (2, 0);
GO
DECLARE @t TABLE (myKey int);
UPDATE MyTest2
SET myValue = 2
    OUTPUT inserted.myKey INTO @t(myKey) 
WHERE myKey = 1 
    AND TS = myValue;
IF (SELECT COUNT(*) FROM @t) = 0
    BEGIN
        RAISERROR ('error changing row with myKey = %d'
            ,16 -- Severity.
            ,1 -- State 
            ,1) -- myKey that was changed 
    END;
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
"timestamp" is deprecated but developers can not use "rowversion" in vs 2008      gerry lowry ... a923921   |   Edit   |   Show History

This is a serious problem. A note above states:

The timestamp syntax is deprecated. This feature will be removed in a future version of Microsoft SQL Server. Avoid using this feature in new development work, and plan to modify applications that currently use this feature.

In vs2008, for example, in the Server Explorer (in Visual Web Developer 2008, it's called the Database Explorer), try to create a rowversion field. You can not!!!

So we have an alias for rowversion that has been deprecated, but we are still forced to use the deprecated alias because the drop down in the table definer does not allow us to use rowversion.

This is a big problem, especially if you intend to remove timestamp in a future version of ms SQL Server.

Regards,
Gerry (Lowry)

[Tai Yee - MSFT] Hi, the best way to submit product feedback is to use the Microsoft Connect site. You can submit your feature wishes for SQL Server at http://connect.microsoft.com/sqlserver. Others can vote on your submissions as well, and the highest voted items are those that are most likely to get implemented in future releases.

@ Tai Yee: Hi Tai, at the same time as I posted "this" annotation, I also submitted a head's up to Scott Guthrie who has escaleted this issue to individuals who, in turn, are liaising with other Microsoft stake holders. It's also important to post such information *here* so that it stands out like a sore thumb to other developers. For VS2010, a fix is likely with the future release of Beta 2.

BTW, voting on *this* issue is unnecessary. When deprecation occurs, there is a requirement for tools to be in place long in advance because of the stated intention "
to remove timestamp in a future version of ms SQL Server" and the Microsoft admonition to "Avoid using this feature in new development work, and plan to modify applications that currently use this feature". gerry 2009 June 4

[Tai Yee - MSFT] thanks for the additional information Gerry.
ok

the sample code is incorrect      Liu An - MSFT   |   Edit   |   Show History
The samples use "TS=myValue" in the where clause, which is wrong. myValue is the name of another column and it should be tsValue meaning "timestamp value" retrieved sometime before.

WHERE myKey = 1
AND TS = myValue;
Tags What's this?: Add a tag
Flag as ContentBug
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks | Privacy Statement
Page view tracker