Search This Blog

Friday, March 29, 2013

Rowversion SQL Server

SQL Server > Types > Rowversion

Rowversion in SQL Server 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.

Note: Use rowversion instead of timestamp wherever possible
Any update made to one row changes also the rowversion value

Example:

CREATE TABLE #tmp
(
    id       INT PRIMARY KEY,
    name     VARCHAR(20),
    rw       ROWVERSION
)
GO

INSERT #tmp(ID, Name) VALUES (1, 'John')
INSERT #tmp(ID, Name) VALUES (2, 'Dan')
GO


SELECT * FROM #tmp

id name rw
1  John  0x0000000000122432
2  Dan   0x0000000000122433

update
   #tmp
set
   name = 'Bill'
where
   id = 1

SELECT * FROM #tmp

id name rw
1  Bill    0x0000000000122434
2  Dan   0x0000000000122433

drop table #tmp