KleberBH
KleberBH

Reputation: 462

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?

Upvotes: 1

Views: 1997

Answers (2)

S3S
S3S

Reputation: 25132

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]))

Upvotes: 2

M. Kanarkowski
M. Kanarkowski

Reputation: 2205

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]))

Upvotes: 0

Related Questions