0

I am trying to delete duplicate or more than duplicate record in my table using this :

DECLARE db_cursor CURSOR FOR SELECT distinct LineNumber FROM SPMS.dbo.Lines
DECLARE @lineNumber VARCHAR(MAX);
DECLARE @lineId int;
DECLARE @count int;
   OPEN db_cursor;
FETCH NEXT 
   FROM db_cursor 
   INTO @lineNumber;
  WHILE @@FETCH_STATUS = 0  
  BEGIN  
    SELECT @count=count(*) 
      FROM [SPMS].[dbo].Lines 
     WHERE @lineNumber=@lineNumber
           if @count>1
    BEGIN   
    SELECT @lineId=Id 
      FROM [SPMS].[dbo].Lines 
     WHERE @lineNumber=@lineNumber
    DELETE FROM [SPMS].[dbo].Lines 
     WHERE id=@lineId 
       END
FETCH NEXT 
      FROM db_cursor INTO @lineNumber;
       END;
     CLOSE db_cursor;
DEALLOCATE db_cursor;

But it delete all same records .why ?

Prabhat Sinha
  • 1,500
  • 20
  • 32
Ehsan Akbar
  • 6,977
  • 19
  • 96
  • 180

0 Answers0