2

Possible Duplicate:
How to delete duplicate rows with SQL?

I have a table with no primary key and a column with duplicate entries. I want to delete all duplicates keeping one entry in the table. Please help

Community
  • 1
  • 1
user1259132
  • 320
  • 2
  • 3
  • 11

3 Answers3

4

Since you are using SQL Server 2005+, you can use CTE to perform this:

;WITH cte AS 
(
  SELECT ROW_NUMBER() OVER (PARTITION BY Col1, Col2, Col3  ORDER BY (SELECT 0)) RN
  FROM  yourtable
)
DELETE FROM cte
WHERE RN > 1
Taryn
  • 242,637
  • 56
  • 362
  • 405
1

Use Row_Number function with Partition By all fields and keep only the rows with RN = 1.

Gidil
  • 4,137
  • 2
  • 34
  • 50
0

Create Stored procedure and inside of procedure:

  1. you have to create temp table from given table structure at dynamically
  2. then insert all distinct row from table to temp table
  3. finally truncate table
  4. and atlast insert row from temp table and drop temp table

If you have problem then I have to write sp for you.

Taryn
  • 242,637
  • 56
  • 362
  • 405
Vikram Jain
  • 5,498
  • 1
  • 19
  • 31