SQL 创建表时出错:“数据库中已经有一个名为...的对象”,但不是具有该名称的对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2523397/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 05:48:48  来源:igfitidea点击:

Error creating a table : "There is already an object named ... in the database", but not object with that name

sqlsql-serversql-server-2005tsql

提问by DavRob60

I'm trying to create a table on a Microsoft SQL Server 2005 (Express).

我正在尝试在 Microsoft SQL Server 2005 (Express) 上创建一个表。

When i run this query

当我运行此查询时

USE [QSWeb]
GO

/****** Object:  Table [dbo].[QSW_RFQ_Log]    Script Date: 03/26/2010 08:30:29 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

SET ANSI_PADDING ON
GO

CREATE TABLE [dbo].[QSW_RFQ_Log](
    [RFQ_ID] [int] NOT NULL,
    [Action_Time] [datetime] NOT NULL,
    [Quote_ID] [int] NULL,
    [UserName] [nvarchar](256) NOT NULL,
    [Action] [int] NOT NULL,
    [Parameter] [int] NULL,
    [Note] [varchar](255) NULL,
 CONSTRAINT [QSW_RFQ_Log] PRIMARY KEY CLUSTERED 
(
    [RFQ_ID] ASC,
    [Action_Time] 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

I got this error message

我收到此错误消息

Msg 2714, Level 16, State 4, Line 2 There is already an object named 'QSW_RFQ_Log' in the database. Msg 1750, Level 16, State 0, Line 2 Could not create constraint. See previous errors.

消息 2714,级别 16,状态 4,第 2 行 数据库中已经有一个名为“QSW_RFQ_Log”的对象。消息 1750,级别 16,状态 0,第 2 行 无法创建约束。请参阅以前的错误。

but if i try to find the object in question using this query:

但如果我尝试使用此查询查找有问题的对象:

SELECT *
FROM QSWEB.sys.all_objects
WHERE upper(name) like upper('QSW_RFQ_%') 

I got this

我懂了

(0 row(s) affected)

(0 行受影响)

What is going on????

到底是怎么回事????

回答by Jamie Ide

You are trying to create a table with the same name as a constraint (QSW_RFQ_Log). Your query doesn't find the object because the table creation fails so the object doesn't exist after the error. Pick a new name for the constraint and it will work, e.g.:

您正在尝试创建一个与约束 (QSW_RFQ_Log) 同名的表。您的查询未找到该对象,因为表创建失败,因此错误后该对象不存在。为约束选择一个新名称,它将起作用,例如:

CONSTRAINT [QSW_RFQ_Log_PK] PRIMARY KEY CLUSTERED

回答by KM.

try this:

尝试这个:

CONSTRAINT [PK_QSW_RFQ_Log] PRIMARY KEY CLUSTERED 
add this    ^^^

you are trying to add the Primary key the same name as the table, make the PK have a different name.

您正在尝试添加与表同名的主键,使 PK 具有不同的名称。

回答by Scoregraphic

You should not name the primary key constraint like your datatable ;-)

你不应该像你的数据表一样命名主键约束;-)