Delete duplicate rows

前端 未结 3 1582
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-14 14:55

I have a table that looks like this

Table1

Id, Name

How can I write a query that delete all rows with duplicate names but kee

3条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-14 15:20

    Simply you can do this with using cursors the query might be like this

    declare @id int declare @name nvarchar(30)

    declare cr cursor for select id,name from idnametbl order by id

    for update

    open cr

    fetch next from cr into @id,@name

    while @@fetch_status=0

    begin

    delete from idnametbl where id> @id and name=@name

    fetch next from cr into @id,@name

    end

    close cr

    deallocate cr

提交回复
热议问题