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 are the difference between primary key and unique key? : sql server database administration

701


What are page splits?

762


How to enter unicode character string literals in ms sql server?

749


Can you explain various data region available in ssrs with their use?

722


Tell me can we use custom code in ssrs?

752






What are the different types of stored procedures?

793


What is sql injection? How to protect against sql injection attack?

736


What are the joins in sql server? : sql server database administration

732


How to access the inserted record of an event?

772


Characterize join and name diverse sorts of joins?

680


You want to generate a report that is formatted as a chart. Can you use the report wizard to create such a report?

105


What is truncate table?

705


How to create a new login name in ms sql server?

703


what is create database syntax? : Sql server database administration

793


What are sql dirty pages?

756