In a table that use system versioning aka Temporal tables, is there a way to modify the time stamp columns (row start, row end, often named ValidFrom and ValidTo) some how?
It can be done in the temporal table itself when system versioning is off and the timestamps updates comply to the row-end-of-previuos-must-equal-row-start-of-next-pattern.
But it doesn't seems to work for the "normal" table itself, even if system versioning is off.
This works for the TT:
ALTER TABLE dbo.[Customer] SET (SYSTEM_VERSIONING = OFF);
update [dbo].[CustomerHistory] set ValidFrom = '2024-03-25 11:21:38.1854297' where [Id] = 5
ALTER TABLE dbo.[Customer] SET (SYSTEM_VERSIONING = ON);
But it fails for the "normal" table
ALTER TABLE dbo.[Customer] SET (SYSTEM_VERSIONING = OFF);
update [dbo].[Customer] set ValidFrom = '2024-03-25 11:21:38.1854297' where [Id] = 5
ALTER TABLE dbo.[Customer] SET (SYSTEM_VERSIONING = ON);
producing the error message: "The GENERATED ALWAYS columns in the "dbo.Customer" table cannot be updated."
Any way to get around it?
I need to copy a bunch of tables 1:1 from one DB to another, and the TimeStamps of the TT / History Tables need to be the ones from the original source database tables...
From the comments, it appears the real question is not how to
UPDATEanALWAYS GENERATEDcolumn, but how to migrate data from a temporal table into a new temporal table. If you need to copy data from a different (temporal) table to a new one, and retain the history dates, then create the new table(s) not as a temporal table and thenALTERit to one after youINSERTall the data. If you start by creating the new table as a temporal table then retaining the Start/End dates isn't possible, as you cannotINSERTinto, orUPDATEthe values of yourGENERATED ALWAYScolumns. See the linked duplicate (as this question was closed after this answer was added), on how to completely disable theGENERATED ALWAYSfeature.This means you create the table, and history tables, not as a temporal table. Such as like below (the
CONSTRAINTs are important):Now you
INSERTall your data into these 2 tables. I'm just going to use some made up data here, it doesn't come from another table:Now all the data is in the 2 tables, we can make the tables temporal:
And now we can do a couple of queries, and get the expected results:
Which returns the following 2 data sets:
Clean up:
db<>fiddle