Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I'm trying to create a table on a 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.

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)

What is going on????

like image 754
DavRob60 Avatar asked Mar 26 '10 13:03

DavRob60


2 Answers

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.:

CONSTRAINT [QSW_RFQ_Log_PK] PRIMARY KEY CLUSTERED
like image 61
Jamie Ide Avatar answered Oct 29 '22 00:10

Jamie Ide


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.

like image 7
KM. Avatar answered Oct 29 '22 02:10

KM.