6

I have an sql server database, that I pre-loaded with a ton of rows of data.

Unfortunately, there is no primary key in the database, and there is now duplicate information in the table. I'm not concerned about there not being a primary key, but i am concerned about there being duplicates in the database...

Any thoughts? (Forgive me for being an sql server newb)

4
  • 2
    Well, howdo you determine a duplicate? Commented Nov 20, 2009 at 19:02
  • 1
    lmgtfy.com/?q=Deleting+duplicate+records+in+SQL+Server Commented Nov 20, 2009 at 19:05
  • Which version of SQL Server are you using ? 2000, 2005, 2008 ? Commented Nov 20, 2009 at 19:08
  • 13
    "I am not concerned with having shot myself in my foot, but I am concerned with all the blood on the floor" Commented Nov 20, 2009 at 19:09

2 Answers 2

13

Well, this is one reason why you should have a primary key on the table. What version of SQL Server? For SQL Server 2005 and above:

;WITH r AS
(
    SELECT col1, col2, col3, -- whatever columns make a "unique" row
    rn = ROW_NUMBER() OVER (PARTITION BY col1, col2, col3 ORDER BY col1)
    FROM dbo.SomeTable
)
DELETE r WHERE rn > 1;

Then, so you don't have to do this again tomorrow, and the next day, and the day after that, declare a primary key on the table.

Sign up to request clarification or add additional context in comments.

Comments

0

Let's say your table is unique by COL1 and COL2.
Here is a way to do it:

SELECT *
FROM (SELECT COL1, COL2, ROW_NUMBER() OVER (PARTITION BY COL1, COL2 ORDER BY COL1, COL2 ASC) AS ROWID
      FROM TABLE_NAME )T
WHERE T.ROWID > 1

The ROWID > 1 will enable you to select only the duplicated rows.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.