Error 2714: There Is Already an Object Named MyFirstTable

Error 2714 says there is already an object with your new table’s name, even when no such table exists. A client hit it while creating a table. I looked into it during their Comprehensive Database Performance Health Check consultation.

Gouache painting of a small blank cream luggage tag tied with a vermilion string to the handle of a brown suitcase, a second suitcase standing behind it

The Error

Msg 2714, Level 16, State 6, Line 5
There is already an object named 'MyFirstTable' in the database.

What the Client Saw

This is the simplified script the client used to create the table:

CREATE TABLE MyFirstTable
(ID INT NOT NULL);

The error said MyFirstTable already existed. The client was sure there was no such table in the database.

How to Check

I asked the client to send me the result of this query:

SELECT *
FROM sys.tables
WHERE name = 'MyFirstTable'

The result was empty, so there was no such table. Then I gave them a query that looks beyond tables:

SELECT *
FROM sys.objects
WHERE name = 'MyFirstTable'

It returned a single row. The object wasn’t a table. It was a primary key constraint on another object, and its parent_object_id was 629577281.

SSMS result grid for sys.objects with one row: name MyFirstTable, object_id 645577338, schema_id 1, parent_object_id 629577281, type PK and type_desc PRIMARY_KEY_CONSTRAINT.

This query gives the name of the parent object:

SELECT OBJECT_NAME(parent_object_id) ParentObject, *
FROM sys.objects
WHERE name = 'MyFirstTable'

SSMS result grid with a ParentObject column: ParentObject is FirstTable, name is MyFirstTable, type is PK and type_desc is PRIMARY_KEY_CONSTRAINT.

Why It Happens

The parent object was FirstTable. I checked its definition, and it held a primary key constraint named MyFirstTable. A constraint is an object too, and object names must be unique within a schema. So the new table couldn’t take that name. This is the definition of the table:

CREATE TABLE FirstTable
(ID INT NOT NULL,
CONSTRAINT MyFirstTable PRIMARY KEY (ID));

The Fix

The solution is simple. Create the new table with a different name, or rename the original primary key to a new name.

How to Prevent It

Name your constraints on purpose, so that a constraint doesn’t take a name you want for a table. If you have questions about this post, tell me in the comments.

Quick reference card: Error 2714: Name Taken

Error 2714 is not a ghost table, it is a primary key that took the name first.

Published by Pinal Dave on SQLAuthority. More of my work at pinaldave.com.


Discover more from SQL Authority with Pinal Dave

Subscribe to get the latest posts sent to your email.

Primary Key, SQL Constraint and Keys, SQL Error Messages, SQL Scripts
Previous Post
AT TIME ZONE Twice: Convert UTC to Local Time in SQL Server
Next Post
Normalisation in Plain Words, and When to Stop

Related Posts

2 Comments. Leave new

  • Ronald Duncan
    March 17, 2022 2:21 pm

    I had a similar bug where I was changing an autogenerated Create table script to add in a primary key

    However when I copied and pasted the new name, I changed what should have been PK_name to name and so was unable to create the constraint and got the There is already an object named ‘name’ in the database for what I thought was the create table statement. :) but was for the index that was part of the create table statement.

    So looking up the object id failed since the table would only be created if the create table ran, and it could not because the 2nd part of the statement creating the index failed since I had given both the same name by accident.

    OK
    CREATE TABLE name(
    [ID] [int] NOT NULL,
    …,
    CONSTRAINT [PK_name] PRIMARY KEY CLUSTERED
    (
    [ID]
    )

    NOT OK
    CREATE TABLE name(
    [ID] [int] NOT NULL,
    …,
    CONSTRAINT [name] PRIMARY KEY CLUSTERED
    (
    [ID]
    )

    Reply
  • Ronald, your reply saved my day, I did your same thing with the same issue, thanks for posting your experience.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.