如何在新的时间 table(系统版本控制)上使用 SSDT 防止 SQL71609

How to prevent SQL71609 with SSDT on a new temporal table (system-versioned)

我尝试在 Visual Studio 2017 年和 SQL 服务器数据工具 (SSDT) 中使用 SQL 服务器时态表。

但我立即收到以下错误:

SQL71609: System-versioned current and history tables do not have matching schemas. Mismatched column: '[dbo].[MyTable].[ValidFrom]'

我没有看到任何错误。我错过了什么吗?

I created a small repository on GIT HUB for reproduction

当前table定义为:

CREATE TABLE [dbo].[MyTable]
(
    [TenantId] UNIQUEIDENTIFIER NOT NULL CONSTRAINT [DF_MyTable_TenantId] DEFAULT 
                            CAST(SESSION_CONTEXT(N'TenantId') AS UNIQUEIDENTIFIER),
    [Rn] BIGINT IDENTITY(1,1) NOT NULL,
    [Id] UNIQUEIDENTIFIER NOT NULL,
    [PropA] INT NOT NULL,
    [PropB] NVARCHAR(100) NOT NULL,

    [ValidFrom] DATETIME2 GENERATED ALWAYS AS ROW START HIDDEN NOT NULL CONSTRAINT [DF_ValidFrom] DEFAULT CONVERT(DATETIME2, '0001-01-01'),
    [ValidTo] DATETIME2 GENERATED ALWAYS AS ROW END HIDDEN NOT NULL CONSTRAINT [DF_ValidTo] DEFAULT CONVERT(DATETIME2, '9999-12-31 23:59:59.9999999'),
    PERIOD FOR SYSTEM_TIME ([ValidFrom], [ValidTo]),

    CONSTRAINT [PK_MyTable] PRIMARY KEY NONCLUSTERED ([Id]),
)
WITH (SYSTEM_VERSIONING = ON (HISTORY_TABLE = [dbo].[MyTableHistory]))
GO

CREATE UNIQUE CLUSTERED INDEX [CIX_MyTable] ON [dbo].[MyTable]([Rn])
GO

和历史 table :

CREATE TABLE [dbo].[MyTableHistory]
(
    [TenantId] UNIQUEIDENTIFIER NOT NULL,
    [Rn] BIGINT IDENTITY(1,1) NOT NULL,
    [Id] UNIQUEIDENTIFIER NOT NULL,
    [PropA] INT NOT NULL,
    [PropB] NVARCHAR(100) NOT NULL,

    [ValidFrom] DATETIME2,
    [ValidTo] DATETIME2,
);
GO
CREATE CLUSTERED COLUMNSTORE INDEX [COLIX_MyTableHistory]
    ON [dbo].[MyTableHistory];
GO
CREATE NONCLUSTERED INDEX [IX_ImpactHistory_ValidFrom_ValidTo_Id]
    ON [dbo].[MyTableHistory] ([ValidFrom], [ValidTo], [Id]);
GO

不太确定您收到此特定错误消息的原因。
我已经在 db fiddle 上测试了你的代码并得到了不同的错误。

顺便说一句,请注意,您不必自己编写历史记录 table - 如果您仅使用 SYSTEM_VERSIONING = ON (HISTORY_TABLE = [dbo].[MyTableHistory]) 设置它的名称而不创建它,SQL Server will generate it automatically for you - as can be seen in this fiddle

对于第一个 attempt 我遇到了这个错误:

Msg 13518 Level 16 State 1 Line 20 Setting SYSTEM_VERSIONING to ON failed because history table 'fiddle_e3d361da65804a39b041c8149132b443.dbo.MyTableHistory' has IDENTITY column specification. Consider dropping all IDENTITY column specifications and trying again.

因此,我从历史 table 和 tried again[Rn] 列中删除了 identity。 然后我得到了这个错误:

Msg 13530 Level 16 State 1 Line 20 Setting SYSTEM_VERSIONING to ON failed because system column 'ValidFrom' in history table 'fiddle_d6660ab11cdc448dba35790867169a14.dbo.MyTableHistory' corresponds to a period column in table 'fiddle_d6660ab11cdc448dba35790867169a14.dbo.MyTable' and cannot be nullable.

所以我将 ValidFromValidTo 列都更改为 NOT NULL,最后 got it working.

工作版本复制到这里:

CREATE TABLE [dbo].[MyTableHistory]
(
    [TenantId] UNIQUEIDENTIFIER NOT NULL,
    [Rn] BIGINT NOT NULL,
    [Id] UNIQUEIDENTIFIER NOT NULL,
    [PropA] INT NOT NULL,
    [PropB] NVARCHAR(100) NOT NULL,

    [ValidFrom] DATETIME2 NOT NULL,
    [ValidTo] DATETIME2 NOT NULL,
);

CREATE CLUSTERED COLUMNSTORE INDEX [COLIX_MyTableHistory]
    ON [dbo].[MyTableHistory];

CREATE NONCLUSTERED INDEX [IX_ImpactHistory_ValidFrom_ValidTo_Id]
    ON [dbo].[MyTableHistory] ([ValidFrom], [ValidTo], [Id]);


CREATE TABLE [dbo].[MyTable]
(
    [TenantId] UNIQUEIDENTIFIER NOT NULL CONSTRAINT [DF_MyTable_TenantId] DEFAULT CAST(SESSION_CONTEXT(N'TenantId') AS UNIQUEIDENTIFIER),
    [Rn] BIGINT IDENTITY(1,1) NOT NULL,
    [Id] UNIQUEIDENTIFIER NOT NULL,
    [PropA] INT NOT NULL,
    [PropB] NVARCHAR(100) NOT NULL,

    [ValidFrom] DATETIME2 GENERATED ALWAYS AS ROW START HIDDEN NOT NULL CONSTRAINT [DF_ValidFrom] DEFAULT CONVERT(DATETIME2, '0001-01-01'),
    [ValidTo] DATETIME2 GENERATED ALWAYS AS ROW END HIDDEN NOT NULL CONSTRAINT [DF_ValidTo] DEFAULT CONVERT(DATETIME2, '9999-12-31 23:59:59.9999999'),
    PERIOD FOR SYSTEM_TIME ([ValidFrom], [ValidTo]),

    CONSTRAINT [PK_MyTable] PRIMARY KEY NONCLUSTERED ([Id]),
)
WITH (SYSTEM_VERSIONING = ON (HISTORY_TABLE = [dbo].[MyTableHistory]))


CREATE UNIQUE CLUSTERED INDEX [CIX_MyTable] ON [dbo].[MyTable]([Rn])