SQL Server 2016临时表为“ GENERATED”抛出不正确的语法

时间:2019-06-07 16:26:50

标签: sql sql-server

帮助,由于某种原因,我无法使它正常工作。

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

我不断收到这些错误:

  

第15层状态1的第24行的消息102
  “ GENERATED”附近的语法不正确。

     

Msg 319,第15层,状态1,第29行
  关键字“ with”附近的语法不正确。如果此语句是公用表表达式,xmlnamespaces子句或更改跟踪上下文子句,则前一条语句必须以分号终止。

     

第159条消息,第15级,状态1,第30行
  关键字“ with”附近的语法不正确。如果此语句是公用表表达式,xmlnamespaces子句或更改跟踪上下文子句,则前一条语句必须以分号终止。

这是SQL Server 2016安装。

有帮助吗?

2 个答案:

答案 0 :(得分:2)

仅需一点语法。在文件组之前,将您的第一个WITH移至与它相关的约束。

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

答案 1 :(得分:0)

您在语法上犯了一个错误。首先WITH与您的索引有关,因此应该在此之后。

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