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


Please Help Members By Posting Answers For Below Questions

What is filtered index?

653


What is sql server used for?

556


How to generate create view script on an existing view?

562


what is an index? : Sql server database administration

523


Explain insert into select statement?

523






Explain alternate key, candidate key and composite key in sql server?

522


Why it is recommended to avoid referencing a floating point column in the where clause?

538


How does a profiler work?

514


Explain foreign key in sql server?

579


Does group by or order by come first?

541


Does order by actually change the order of the data in the tables or does it just change the output?

672


How do you implement session management in SQL Server mode?

558


How to receive output values from stored procedures?

483


What is the use of sql profiler in sql server 2012?

513


OPTIMIZATION OF SP,CURSOR,TRIGGERS

2209