3

此 SQL 查询是由 SQL Server Management Studio 生成的,它给我一个错误:

USE [database_name]
GO
/****** Object:  Table [dbo].[UserAddress]    Script Date: 02/17/2010 11:21:02 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO

CREATE TABLE [dbo].[UserAddress]
(
    [ID] [int] IDENTITY(1,1) NOT NULL,
    [UserID] [int] NULL,
    [AddressName] [nvarchar](25) NULL,
    [Fname] [nvarchar](15) NULL,
    [LName] [nvarchar](20) NULL,
    [City] [nvarchar](15) NULL,
    [Street] [nvarchar](30) NULL,
    [StreetNum] [nvarchar](5) NULL,
    [FloorNum] [int] NULL,
    [AptNum] [int] NULL,
    [ZipCode] [int] NULL,
    [Phone] [varchar](15) NULL,
    [Phone_Prefix] [int] NULL,
    [CellPhone] [varchar](15) NULL,
    [CellPhone_Prefix] [int] NULL,
    [Fax] [varchar](15) NULL,
    [Fax_Prefix] [int] NULL,
    [Primary] [bit] NULL,
    CONSTRAINT [PK_UserAddress] PRIMARY KEY CLUSTERED 
    (
        [ID] ASC
    )
    WITH
    (
        PAD_INDEX  = OFF,
        STATISTICS_NORECOMPUTE  = OFF,
        IGNORE_DUP_KEY = OFF,
        ALLOW_ROW_LOCKS  = ON,
        ALLOW_PAGE_LOCKS  = ON
    ) ON [PRIMARY]
) ON [PRIMARY]

GO
SET ANSI_PADDING OFF

错误是:Msg 170, Level 15, State 1, Line 27 Line 27: Incorrect syntax near '('.指向[CellPhone_Prefix] [int] NULL,但这条线对我来说很好。
有什么问题?
编辑:
我刚刚注释掉了

 CONSTRAINT [PK_Users] PRIMARY KEY CLUSTERED 
(
    [ID] ASC,
    [ClientStoreID] ASC,
    [Uname] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]

现在它起作用了,为什么?
编辑2:
我将其缩小到:

WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]  

我在这里错过了什么吗?

4

1 回答 1

9

我认为这取决于 SQL Server 数据库版本。

我尝试使用 SSMS 2005 对 SQL Server 2000 数据库进行查询,但它失败并出现您描述的相同错误。

当我连接到 SQL Server 2005 服务器时,查询完美执行。

您是否确实检查过您的服务器版本,而不仅仅是您的 SSMS 版本。

根据SQL Server 2000语法, WITH 部分只允许设置 FillFactor,没有其他内容:

< table_constraint > ::= [ CONSTRAINT constraint_name ]
{ [ { PRIMARY KEY | UNIQUE }
    [ CLUSTERED | NONCLUSTERED ]
    { ( column [ ASC | DESC ] [ ,...n ] ) }
    [ WITH FILLFACTOR = fillfactor ]
    [ ON { filegroup | DEFAULT } ]
] 

这与SQL Server 2008 / 2005语法不同,后者允许括号内有多个选项:

< table_constraint > ::= [ CONSTRAINT constraint_name ] 
{  { PRIMARY KEY | UNIQUE } 
    [ CLUSTERED | NONCLUSTERED ] 
    (column [ ASC | DESC ] [ ,...n ] ) 
    [ WITH FILLFACTOR = fillfactor | WITH ( <index_option> [ , ...n ] ) ]
    [ ON { partition_scheme_name (partition_column_name) | filegroup | "default" } ] 
    .
    .
    .
}
于 2010-02-17T10:08:32.987 回答