Adding a rowversion column to a table and ordering existing data

1.9k Views Asked by At

I want to add a rowversion column to an existing table in my database so I can essentially order by the last time each record was modified successfully. My question is, how will adding the rowversion affect my existing data? Will the rowversion values be assigned like random on these records or by the last instance they were modified too? (Even though they existed before the column)

1

There are 1 best solutions below

0
David Browne - Microsoft On

My question is, how will adding the rowversion affect my existing data?

You can test it.

eg

drop table if exists t
go
create table t(id int primary key)
insert into t(id) values (1),(2),(3)
go
alter table t add rv rowversion

go
insert into t(id) values (4)

insert into t(id) values (5),(6)

go
select * from t

outputs

(3 rows affected)

(1 row affected)

(2 rows affected)
id          rv
----------- ------------------
1           0x00000000000007DD
2           0x00000000000007DE
3           0x00000000000007DF
4           0x00000000000007E0
5           0x00000000000007E1
6           0x00000000000007E2

(6 rows affected)