Schema - Do you have a rowversion column?

Updated by Brady Stroud [SSW] 1 year ago. See history

123
<introEmbed body={<> SQL Server rowversions are a data type available which are binary numbers that indicate the relative sequence in which data modifications took place in a database. See the MSDN article on rowversions here: [rowversion (Transact-SQL)](https://docs.microsoft.com/en-us/sql/t-sql/data-types/rowversion-transact-sql?view=sql-server-ver15&WT.mc_id=DP-MVP-33518) </>} />

All tables should have a rowversion column called "RecordVersion" to aid concurrency checking. A rowversion improves update performance because only one column needs to be checked when performing a concurrency check (instead of checking all columns in a table for changes).

❌ Figure: Figure: Bad Example - No rowversion available in this table

CREATE TABLE MyTest (myKey int PRIMARY KEY
,myValue int, RecordVersion rowversion);
GO
INSERT INTO MyTest (myKey, myValue) VALUES (1, 0);
INSERT INTO MyTest (myKey, myValue) VALUES (2, 0);
INSERT INTO MyTest (myKey, myValue) VALUES (3, 0);
UPDATE MyTest SET myValue = 1 WHERE myKey = 2
SELECT * FROM MyTest ORDER BY RecordVersion DESC

✅ Figure: Figure: Good Example - A create statement which builds a table with a rowversion

✅ Figure: Figure: Good Example - A set of records with a rowversion available

Acknowledgements

Adam Cogan
Alex Breskin
Christian Morford-Waite
Related rules

Need help?

SSW Consulting has over 30 years of experience developing awesome software solutions.

We open source.Loving SSW Rules? Star us on GitHub. Star