Detecting and Deleting Duplicate Records in SQL Server 2005 and Above

Submitted by: 
Language: 
Visitors have accessed this post 5048 times.

Most of the time, we encounter duplicate records in our database. And it's not easy to delete it if you don't have enough knowledge about SQL Statement.

This code will help you detect and delete duplicate records in SQL Server 2005 and above.

(These commands only works on sql server 2005 or higher)

Create table

  1. CREATE TABLE example(id INT IDENTITY(1,1),name VARCHAR(20),class VARCHAR(20));

(Here Identity is used for auto increment of values starting from 1)

Insert Records

  1.  
  2. INSERT INTO example(name,class)VALUES('tam','1');
  3. INSERT INTO example(name,class)VALUES('tam','1');
  4. INSERT INTO example(name,class)VALUES('ram','1');
  5. INSERT INTO example(name,class)VALUES('tam','2');
  6. INSERT INTO example(name,class)VALUES('tam','1');
  7. INSERT INTO example(name,class)VALUES('ram','1');
  8. INSERT INTO example(name,class)VALUES('ram','2');
  9. INSERT INTO example(name,class)VALUES('ram','2');

Here we are inserting records. It contains duplicates also.

For the detection and Deletion of duplicates we use Common Table Expressions (CTE).

Detecting Duplicates

  1. SELECT ROW_NUMBER() OVER(partition BY name,class ORDER BY id) AS dup FROM example;

If you get more than 1 in any of the columns, that means you have duplicate records in your Table.

Deleting Duplicates

  1. WITH sample1
  2. AS
  3. (SELECT ROW_NUMBER() OVER(partition BY name,class ORDER BY id) AS dup FROM example)
  4. DELETE FROM sample1 WHERE dup>1
  5. GO

With this code snippet you can delete duplicates from your table.
(Here Sample1 is any name, it is a CTE name).


Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.