Delete duplicates rows T-SQL

While loop statement

To delete duplicates rows in a tabe you need to use a while loop statement in a cursor.

Delete duplicates rows

USE model;
GO
declare @Cursor_S cursor;
declare @ID int;
declare @CN int;
begin
  set @Cursor_S = cursor for
  select id, count(*)-1 CN
  from Students_Math
  group by id
  having count(*) > 1;
  open @Cursor_S
  fetch next from @Cursor_S
  into @ID, @CN
  while @@FETCH_STATUS = 0
  begin
     set rowcount @CN
     delete from Students_Math where id = @ID
     set rowcount 0
     fetch next from @Cursor_S
     into @ID, @CN
  end;
  close @Cursor_S ;
  deallocate @Cursor_S;
end;
GO