SQL

5 pts.
Tags:
Duplicate records
SQL
I have some duplicate rows in my SQL database table, Please Tell me how'll delete duplicate

Answer Wiki

Thanks. We'll let you know when a new response is added.

Without a primary key there’s no fast easy way to do this. You’ll need to handle each one, one at a time.

If you are using SQL 2000 or below you’ll need to do something like this.

<pre>SET ROWCOUNT 1
DELETE FROM YourTable
WHERE Col1 = ‘Something’
AND COl2 = ‘SomethingElse’
/*Add what ever filters are needed to isolate the duplicate row*/</pre>

If you are using SQL 2005 or above you’ll ned to do something like this.

<pre>
DELETE TOP (1) FROM YourTable
WHERE Col1 = ‘Something’
AND COl2 = ‘SomethingElse’
/*Add what ever filters are needed to isolate the duplicate row*/</pre>

Discuss This Question: 2  Replies

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
  • Denny Cherry
    Check out my SQL Server blog "SQL Server with Mr Denny" for more SQL Server information.
    66,130 pointsBadges:
    report
  • Maxxx13
    I ran into this issue recently and solved it using a select distinct statement against all the columns to make a new table with unique rows as a materialized query. In my case I was then able to confirm the contents of the new table, delete the original and rename the new table. It would be more complicated if there are dependencies on the table, though. SELECT DISTINCT (column1, column 2, column 3, column 4,..., column n) FROM Table I'd also recommend adding a primary key.
    105 pointsBadges:
    report

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

To follow this tag...

There was an error processing your information. Please try again later.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Thanks! We'll email you when relevant content is added and updated.

Following