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.

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

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.

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://docs.microsoft.com/en-us/sql/relational-databases/tables/temporal-tables?view=sql-server-2017

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.