I've been looking into temporal tables recently and I've applied them successfully to one of my tables. I need to apply it to other tables in the database so I ran a script to which was like the example below. However when running it on my 2nd table I get a
Msg 2714, Level 16, State 5, Line 1 There is already an object named 'DF_ValidFrom' in the database.
The article below seems to imply you can use the same constraint across the Product and Location and tables. Anyone got any experience in altering existing tables to temporal tables?
ALTER TABLE Product
ADD
SysStartTime datetime2 (2) GENERATED ALWAYS AS ROW START HIDDEN
constraint DF_ValidFrom DEFAULT DATEADD(second, -1, SYSUTCDATETIME())
, SysEndTime datetime2 (2) GENERATED ALWAYS AS ROW END HIDDEN
constraint DF_ValidTo DEFAULT '9999.12.31 23:59:59.99'
, PERIOD FOR SYSTEM_TIME (SysStartTime, SysEndTime);
ALTER TABLE Product
SET (SYSTEM_VERSIONING = ON (HISTORY_TABLE = dbo.ProductHistory));
ALTER TABLE [Location]
ADD
SysStartTime datetime2 (2) GENERATED ALWAYS AS ROW START HIDDEN
constraint DF_ValidFrom DEFAULT DATEADD(second, -1, SYSUTCDATETIME())
, SysEndTime datetime2 (2) GENERATED ALWAYS AS ROW END HIDDEN
constraint DF_ValidTo DEFAULT '9999.12.31 23:59:59.99'
, PERIOD FOR SYSTEM_TIME (SysStartTime, SysEndTime);
ALTER TABLE [Location]
SET (SYSTEM_VERSIONING = ON (HISTORY_TABLE = dbo.LocationHistory));
Full article is here https://learn.microsoft.com/en-us/sql/relational-databases/tables/temporal-table-usage-scenarios
Thanks in advance for any help.