删除我的sql server中的重复记录

时间:2016-08-08 06:48:05

标签: sql-server

我正在尝试使用以下方法删除表格中的重复或重复记录:

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;

但它删除了所有相同的记录。为什么?

0 个答案:

没有答案