I need to seed data for my local development purpose in the following Temporal Table, the start date should be old. The given Table Schema is
CREATE TABLE [dbo].[Contact](
[ContactID] [uniqueidentifier] NOT NULL,
[ContactNumber] [nvarchar](50) NOT NULL,
[SequenceID] [int] IDENTITY(1,1) NOT NULL,
[SysStartTime] [datetime2](0) GENERATED ALWAYS AS ROW START NOT NULL,
[SysEndTime] [datetime2](0) GENERATED ALWAYS AS ROW END NOT NULL,
CONSTRAINT [PK_Contact] PRIMARY KEY NONCLUSTERED
(
[ContactID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY],
PERIOD FOR SYSTEM_TIME ([SysStartTime], [SysEndTime])
) ON [PRIMARY]
WITH
(
SYSTEM_VERSIONING = ON (HISTORY_TABLE = [dbo].[ContactHistory] , DATA_CONSISTENCY_CHECK = ON )
)
I need to Insert some old dated data into this table.
INSERT INTO dbo.Contact
(
ContactID,
ContactNumber,
--SequenceID - this column value is auto-generated
SysStartTime,
SysEndTime
)
VALUES
(
NEWID(), -- ContactID - uniqueidentifier
N'9999912345', -- ContactNumber - nvarchar
-- SequenceID - int
'2017-09-01 06:26:59', -- SysStartTime - datetime2
NULL -- SysEndTime - datetime2
)
I'm getting the following Error.
Cannot insert an explicit value into a GENERATED ALWAYS column in table 'DevDB.dbo.Contact'. Use INSERT with a column list to exclude the GENERATED ALWAYS column, or insert a DEFAULT into GENERATED ALWAYS column.
Kindly assist me how to add or Update a old dataed data into this Temporal Table
Finally I found a solution
Step #1: Need to Switch it OFF the
SYSTEM_VERSIONING
Step #2: Need to drop the
PERIOD FOR SYSTEM_TIME
Step #3: Insert required record with past date
Step #4: Need to Add the
PERIOD FOR SYSTEM_TIME
Step #5: Need to Switch it ON the
SYSTEM_VERSIONING
That's it...
You can not insert old data timestamp into the temporal table directly. The values
SysStartDate
andSysEndDate
are updated automatically as you work with the table. One possible workaround is to insert test data as you do:and then iteratively process
UPDATES
Note Sql server 2016 introduced only
System-Versioned Temporal Tables
from ANSI Standard 2011 which defines 3 types of temporal tablesSystem-Versioned Temporal Tables do not support changes in the already registered history by design. So to create a test temporal data set you need to re-execute all hystory INSERTs in an order.
What are you trying to achieve? The GENERATED ALWAYS columns are technical columns and if you set them as that you can't update them, they are updated automatically. For tracking changes you have ContactHistory table.
Normally you are supposed to use following INSERTs:
Then after the first insert this is what you have:
You do not have any records in history yet as it doesn't store the actual record. Now if you would like to change the data, then you use normal UPDATE statements ignoring these GENERATED ALWAYS columns:
Let's check the data again:
Now you have a bit different situation:
As you can see that the actual data is updated as expected and the history table has the old record with the "closed" EndTime.
So, if you would like to have native support of the SCD then Sql Server does everything for, just do not touch these columns. If for the some special reasons you need to update these columns, then just do not use GENERATED ALWAYS columns and use DEFAULT constrains for that.