在数据库的操作中我们经常会遇到表中一些字段重复,也许是标题重复,也许是内容重复,没关系有了这个游标你只需要修改相应的需要被检测的字段,@title 以及表名tsys_News 然后执行便可去掉所有重复项。
declare @max integer,@title varchar(200)
declare cur_rows cursor local for select title,count(*) from tsys_News group by title having count(*) > 1
open cur_rows
fetch cur_rows into @title,@max
while @@fetch_status=0
begin
select @max = @max -1
set rowcount @max
delete from tsys_News where title = @title
fetch cur_rows into @title,@max
end
close cur_rows
set rowcount 0