if 3 duplicate records in table,i want to delete 2 alternating
duplicate records by keeping 1 duplicate and 1 original as it
is,how?
Answer Posted / mohamed ibrahim
Deleting multiple duplicate rows in a table
Ex . I have the Table named as TestMaster
to delete duplicate rows from the testmaster using Cursor &
RANK() Function.
for ex.the table having the fields ID,Name
the having the following data
oupput:
ID NAME
1 Raja
1 Raja
1 Raja
2 Mohamed
2 Mohamed
2 Mohamed
To Delete duplicate Rows in table to follow the below code:
DECLARE @ID INT
DECLARE delduplicaterecords_Cursor CURSOR
FOR SELECT ID FROM TempMaster
OPEN delduplicaterecords_Cursor
FETCH NEXT FROM delduplicaterecords_Cursor INTO @ID
WHILE @@FETCH_STATUS = 0
BEGIN
WITH CTE
AS
(SELECT
ROW_NUMBER () OVER (ORDER BY ID) AS RowID,
*
FROM TempMaster WHERE ID=@ID )
DELETE FROM CTE WHERE RowID <> 1
FETCH NEXT FROM delduplicaterecords_Cursor INTO @ID
END
CLOSE delduplicaterecords_Cursor
DEALLOCATE delduplicaterecords_Cursor
| Is This Answer Correct ? | 0 Yes | 0 No |
Post New Answer View All Answers
What is index, cluster index and nonclustered index?
What is read committed?
Tell me the use of keyword with encryption. Create a store procedure with encryption?
where the connection string store in the database
What are the differences between having and where clause.
How adventureworkslt tables are related?
Why do we use sql limitations? Which constraints can we use while making a database in sql?
Can group by and orderby be used together?
How to insert and update data into a table with "insert" and "update" statements?
What is a View ? Can we insert, Update and delete a view?
Explain about system database?
What command do we use to rename a db, a table and a column?
What is in place upgrade in sql server?
Do you know what are pages and extents? : SQL Server Architecture
Write down the syntax and an example for create, rename and delete index?