kee
kee

Reputation: 13

Error in SQL: The object name already exists in the database

I keep getting this error message in SQL when trying to execute. "Msg 2714, Level 16, State 6, Line 4 There is already an object named 'STUDENT_ACTIVITY' in the database." Prior to this I was getting an error message saying that my database I just created already existed. I put drop database at the beginning which helped that problem, but this new one popped up in its place.

Here is what my code looks like so far:

DROP DATABASE CIT230_02
CREATE DATABASE CIT230_02

CREATE TABLE STUDENT_ACTIVITY
  (
    StudentID INT NOT NULL,
    Activity VARCHAR(45) NULL,
    ActivityFee Numeric(6,2) NULL
  );

  SELECT *
  FROM STUDENT_ACTIVITY;

I've tried renaming the database and the table but I keep getting the same error. I have even tried deleting the software and redownloading it.

Any advice/help would be greatly appreciated, thank you!!

Upvotes: 1

Views: 1578

Answers (1)

nbk
nbk

Reputation: 49410

It is not enough to CREATE a databse, you need after creating also to USE it

DROP DATABASE IF EXISTS CIT230_02
GO
CREATE DATABASE CIT230_02
GO
USE CIT230_02
GO
CREATE TABLE STUDENT_ACTIVITY
  (
    StudentID INT NOT NULL,
    Activity VARCHAR(45) NULL,
    ActivityFee Numeric(6,2) NULL
  );

  SELECT *
  FROM STUDENT_ACTIVITY;

Upvotes: 1

Related Questions