如何在新的临时表上阻止带SSDT的SQL71609(系统版本)

时间:2019-02-18 08:58:30

标签: sql-server temporal temporal-tables

我尝试在Visual Studio 2017和SQL Server数据工具(SSDT)中使用SQL Server临时表。

但是我立即收到以下错误消息:

  

SQL71609:系统版本的当前表和历史表没有   匹配的架构。列不匹配:“ [dbo]。[MyTable]。[ValidFrom]”

我没有看到任何错误。我想念什么吗?

I created a small repository on GIT HUB for reproduction

当前表定义为:

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

历史记录表:

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

1 个答案:

答案 0 :(得分:0)

不太确定为什么会收到此特定错误消息。
我已经在db fiddle上测试了您的代码,并遇到了其他错误。

顺便说一句,请注意,您不必自己编写历史记录表-如果仅使用SYSTEM_VERSIONING = ON (HISTORY_TABLE = [dbo].[MyTableHistory])设置它的名称而不用创建SQL Server will generate it automatically即可,如您所见在this fiddle中。

对于第一个attempt,我遇到了以下错误:

  

消息13518的16级状态1行20   将SYSTEM_VERSIONING设置为ON失败,因为历史记录表'fiddle_e3d361da65804a39b041c8149132b443.dbo.MyTableHistory'具有IDENTITY列规范。考虑删除所有IDENTITY列规范,然后重试。

因此,我从历史记录表和tried againidentity列中删除了[Rn]。 然后我得到了这个错误:

  

消息13530的16级状态1行20   将SYSTEM_VERSIONING设置为ON失败,因为历史记录表'fiddle_d6660ab11cdc448dba35790867169a14.dbo.MyTableHistory'中的系统列'ValidFrom'对应于表'fiddle_d6660ab11cdc448dba35790867169169a14.dbo.MyTable'中的期间列,并且不能为空。

因此,我将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])