教你几种在SQL Server删除重复数据方法
2022-11-12 09:54:14
内容摘要
这篇文章主要为大家详细介绍了教你几种在SQL Server删除重复数据方法,具有一定的参考价值,可以用来参考一下。
对此感兴趣的朋友,看看idc笔记做的技术笔记!方法一
代码如下:
文章正文
这篇文章主要为大家详细介绍了教你几种在SQL Server删除重复数据方法,具有一定的参考价值,可以用来参考一下。
对此感兴趣的朋友,看看idc笔记做的技术笔记!
方法一代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | <code> declare @max integer,@id integer declare cur_rows cursor local for select 主字段, count (*) from 表名 group by 主字段 having count (*) > 1 open cur_rows fetch cur_rows into @id,@max while @@fetch_status=0 begin select @max = @max -1 set rowcount @max delete from 表名 where 主字段 = @id fetch cur_rows into @id,@max end close cur_rows set rowcount 0 </code> |
代码如下:
1 2 | <code>select distinct * from tableName </code> |
代码如下:
1 2 3 4 5 6 | <code> select distinct * into #Tmp from tableName drop table tableName select * into tableName from #Tmp drop table #Tmp </code> |
代码如下:
1 2 3 4 5 | <code> select identity(int,1,1) as autoID, * into #Tmp from tableName select min(autoID) as autoID into #Tmp2 from #Tmp group by Name,autoID select * from #Tmp where autoID in(select autoID from #tmp2) </code> |
代码如下:
1 2 | <code> delete from 表名 where 重复字段名 in (select 重复字段名 from 表名 group by 重复字段名 having count (重复字段名) > 1) and id not in (select min(id) from 表名 group by 重复字段名 having count (重复字段名 )>1) </code> |
注:关于教你几种在SQL Server删除重复数据方法的内容就先介绍到这里,更多相关文章的可以留意
代码注释