Seed data with old dates in Temporal Table - SQL Server

后端 未结 4 553
没有蜡笔的小新
没有蜡笔的小新 2020-12-16 01:00

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 [d         


        
4条回答
  •  时光说笑
    2020-12-16 01:24

    Finally I found a solution

    Step #1: Need to Switch it OFF the SYSTEM_VERSIONING

    ALTER TABLE dbo.Contact SET (SYSTEM_VERSIONING = OFF);
    

    Step #2: Need to drop the PERIOD FOR SYSTEM_TIME

    ALTER TABLE dbo.Contact DROP PERIOD FOR SYSTEM_TIME
    

    Step #3: Insert required record with past date

    INSERT INTO dbo.Contact
    (
        ContactID,
        ContactNumber,
        SysStartTime,
        SysEndTime
    )
    VALUES
    (
        NEWID(), -- ContactID - uniqueidentifier
        N'1234567890', -- ContactNumber - nvarchar
        '2014-09-13 00:00:00', -- SysStartTime - datetime2
        '9999-12-31 23:59:59' -- SysEndTime - datetime2
    )
    

    Step #4: Need to Add the PERIOD FOR SYSTEM_TIME

    ALTER TABLE dbo.Contact
    ADD PERIOD FOR SYSTEM_TIME ([SysStartTime], [SysEndTime])
    

    Step #5: Need to Switch it ON the SYSTEM_VERSIONING

    ALTER TABLE dbo.[Contact] SET (SYSTEM_VERSIONING = ON
     (HISTORY_TABLE=dbo.[ContactHistory],DATA_CONSISTENCY_CHECK=ON)
    );
    

    That's it...

提交回复
热议问题