简体   繁体   中英

How do I create system versioned tables using SSDT in Visual Studio 2019?

I'm trying to create Table with system versioning using Database Project.

  1. Following schema gives error:

    SQL70633: System-versioned temporal table must have history table name explicitly provided.

     CREATE TABLE [dbo].[Products] ( [Id] INT NOT NULL PRIMARY KEY, [Name] NVARCHAR(255) NOT NULL, [ModifiedBy] NVARCHAR(127) NULL ) WITH (SYSTEM_VERSIONING = ON) GO
  2. With explicit name:

    SQL71501: Table: [dbo].[Products] has an unresolved reference to Table [history].[ProductsHistory].
    SQL46010: Incorrect syntax near ].

     CREATE TABLE [dbo].[Products] ( [Id] INT NOT NULL PRIMARY KEY, [Name] NVARCHAR(255) NOT NULL, [ModifiedBy] NVARCHAR(127) NULL ) WITH (SYSTEM_VERSIONING = ON (HISTORY_TABLE = [history].ProductsHistory)) GO

I've tried both, latest version of Visual Studio 2019 (16.7.5) and latest preview (16.8.0 Preview 3.2).

The syntax in both cases is invalid. Executing the first query in SSMS returns:

Cannot set SYSTEM_VERSIONING to ON when SYSTEM_TIME period is not defined.

The command needs a PERIOD FOR SYSTEM_TIME clause specifying the columns used to specify the validity period of a record.

The documentation examples show how to create a temporal table with a default, automatically named history table :

CREATE TABLE [dbo].[Products]
 (
     [Id] INT NOT NULL PRIMARY KEY, 
     [Name] NVARCHAR(255) NOT NULL, 
     [ModifiedBy] NVARCHAR(127) NULL,

     SysStartTime DATETIME2 GENERATED ALWAYS AS ROW START NOT NULL,
     SysEndTime DATETIME2 GENERATED ALWAYS AS ROW END NOT NULL,
     PERIOD FOR SYSTEM_TIME (SysStartTime,SysEndTime)
 )
 WITH (SYSTEM_VERSIONING = ON)

In this case, the SysStartTime and SysEndTime are used to specify the validity period of a record.

Similar syntax is needed to create a temporal table with a user-specified table name

create TABLE [dbo].[Products]
 (
     [Id] INT NOT NULL PRIMARY KEY, 
     [Name] NVARCHAR(255) NOT NULL, 
     [ModifiedBy] NVARCHAR(127) NULL,

     SysStartTime DATETIME2 GENERATED ALWAYS AS ROW START NOT NULL,
     SysEndTime DATETIME2 GENERATED ALWAYS AS ROW END NOT NULL,
     PERIOD FOR SYSTEM_TIME (SysStartTime,SysEndTime)
 )
 WITH (SYSTEM_VERSIONING = ON (HISTORY_TABLE = dbo.ProductHistory))

It's possible to create the history table on a different schema, eg history , as long as that schema exists, BUT it's probably not a good idea unless this solves some specific problem. The current and history table represent the same entity, depend on each other and have specific security restrictions so storing them in separate schemas can make life harder.

To create the table in a separate schema, first create the schema :

CREATE SCHEMA history

Then use the schema in the table definition:

 WITH (SYSTEM_VERSIONING = ON (HISTORY_TABLE = history.ProductHistory))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM