简体   繁体   中英

SQL Server 2016 Temporal table Throwing Incorrect Syntax for “GENERATED”

Help, I can't get this to work for some reason.

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[MYTABLE]
(
    [MyTableId] [INT] IDENTITY(1,1) NOT NULL,
    [Description] [NVARCHAR](255) NULL,
    [DisplayOrder] [INT] NOT NULL,
    [VALIDFROM] [DATETIME2](7) GENERATED ALWAYS AS ROW START NOT NULL,
    [VALIDTO] [DATETIME2](7) GENERATED ALWAYS AS ROW END NOT NULL,
    PERIOD FOR SYSTEM_TIME ([VALIDFROM], [VALIDTO]),
    CONSTRAINT [PK_MYTABLE] PRIMARY KEY CLUSTERED ([MyTableId] ASC)
)  ON [PRIMARY]
WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
WITH (SYSTEM_VERSIONING = ON (HISTORY_TABLE = [dbo].[MYTABLE_History]))
GO

I keep getting these errors:

Msg 102, Level 15, State 1, Line 24
Incorrect syntax near 'GENERATED'.

Msg 319, Level 15, State 1, Line 29
Incorrect syntax near the keyword 'with'. If this statement is a common table expression, an xmlnamespaces clause or a change tracking context clause, the previous statement must be terminated with a semicolon.

Msg 319, Level 15, State 1, Line 30
Incorrect syntax near the keyword 'with'. If this statement is a common table expression, an xmlnamespaces clause or a change tracking context clause, the previous statement must be terminated with a semicolon.

This is SQL Server 2016 install.

Any help?

Just a little off on the syntax. Move your first WITH to your constraint as it is tied to it, before the file group.

CREATE TABLE [dbo].[MYTABLE](
    [MyTableId] [int] IDENTITY(1,1) NOT NULL,
    [Description] [nvarchar](255) NULL,
    [DisplayOrder] [int] NOT NULL,
    [VALIDFROM] [datetime2](7) GENERATED ALWAYS AS ROW START NOT NULL,
    [VALIDTO] [datetime2](7) GENERATED ALWAYS AS ROW END NOT NULL,
    PERIOD FOR SYSTEM_TIME ([VALIDFROM], [VALIDTO]),
    CONSTRAINT [PK_MYTABLE] 
        PRIMARY KEY CLUSTERED ([MyTableId] ASC)
        WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)
) ON [PRIMARY]
 WITH (SYSTEM_VERSIONING = ON (HISTORY_TABLE = [dbo].[MYTABLE_History]))

You did a mistake in the syntax. First WITH pertains to your index so it should be after this.

CREATE TABLE [dbo].[MYTABLE](
    [MyTableId] [int] IDENTITY(1,1) NOT NULL,
    [Description] [nvarchar](255) NULL,
    [DisplayOrder] [int] NOT NULL,
    [VALIDFROM] [datetime2](7) GENERATED ALWAYS AS ROW START NOT NULL,
    [VALIDTO] [datetime2](7) GENERATED ALWAYS AS ROW END NOT NULL,
    PERIOD FOR SYSTEM_TIME ([VALIDFROM], [VALIDTO]),
    CONSTRAINT [PK_MYTABLE] PRIMARY KEY CLUSTERED ([MyTableId] ASC) WITH (PAD_INDEX = OFF,STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)
) ON [PRIMARY] with (SYSTEM_VERSIONING = ON (HISTORY_TABLE = [dbo].[MYTABLE_History]))

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