数据库中已经有一个名为“X”的对象。但事实并非如此

时间:2013-11-20 20:55:10

标签: sql database sql-server-2008

当我尝试运行一个简单的创建表脚本时,我得到“已经有一个对象......”错误。然后当我运行select:

SELECT * FROM dbo.sysobjects WHERE id = object_id (N'[dbo].[tableX]')

没有返回任何行。

以下是我的创建脚本:

USE [DB]
GO   

SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[tableX](
    [id] [int] IDENTITY(1,1) NOT NULL,
    [colA] [int] NOT NULL,
    [colB] [nvarchar](500) NOT NULL,
    [colC] [nvarchar](500) NOT NULL,    
    [colD] [nvarchar](max) NOT NULL,
    [colE] [int] NOT NULL,
 CONSTRAINT [tableX] 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] TEXTIMAGE_ON [PRIMARY]

GO

1 个答案:

答案 0 :(得分:1)

好吧,您似乎正在尝试创建一个与您的表名完全相同的CONSTRAINT

CONSTRAINT [tableX] PRIMARY KEY CLUSTERED

将其更改为:

CONSTRAINT [pk_tableX] PRIMARY KEY CLUSTERED