1

I have a table in a database with three columns: ID, Key and Value. Everytime I update the record, I have a trigger that sends these updates to another table and creates a new record in it, who's schema is this:

RowID (new ID field for this table, identity(1,1) column)
ID (from source table)
Key (from source table)
Value (from source table)
Version (integer)

So anytime the first table record's changed, it creates a new record in the second with the table everytime.

For every record with a matching ID, I want to add a unique incremental version number. If a record with ID of 1 is entered into this table 5 times, those records should have version numbers of 1, 2, 3, 4, and 5. Is the way to accomplish this through a subquery for existing version numbers, or setting up a partition, or something else?

Thanks.

asked Mar 27, 2012 at 13:58
0

3 Answers 3

1

In such cases I am storing version numbers, and using referential integrity to make sure version numbers begin with 1 and have no gaps. Because I use constraints, I am sure my solution works in high concurrency situations. I have described this approach in this article

answered Mar 27, 2012 at 15:57
0

Can't you add rowversion(http://msdn.microsoft.com/en-us/library/ms182776(v=sql.105).aspx) column to your audited table and store this number in another table? It's sequential by definition, though it doesn't start with 1.

answered Mar 27, 2012 at 16:29
0

there is a built-in feature for this that requires minimum maintenance and is very fast except it works with timestamps.

if you can work with timestamps instead of the version I highly recommend this over manual work.

https://learn.microsoft.com/en-us/sql/relational-databases/tables/temporal-tables?view=sql-server-2017

answered Aug 10, 2019 at 13:00
0

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.