手抄报 安全手抄报 手抄报内容 手抄报图片 英语手抄报 清明节手抄报 节约用水手抄报

SQL Server数据库中如何查询重复数据并处理

时间:2024-10-12 03:22:45

1、数据准备:生成重复数据SELECT name,object_id,type,type_desc,create_date,is_ms_shipped into temp_test FROM sys.objectsinsert into temp_testSELECT top 30 name,object_id,type,type_desc,create_date,is_ms_shipped FROM sys.objects

SQL Server数据库中如何查询重复数据并处理

2、验证是否有重复数据方法一:select name,object_id,type,type_desc,create_date,is_ms_shipped,count(*) from temp_testgroup by name,object_id,type,type_desc,create_date,is_ms_shipped having count(*) >1

SQL Server数据库中如何查询重复数据并处理

3、验证是否有重复数据方法二:衡痕贤伎select * from (SELECT name,object_id,type,type_desc,cre锾攒揉敫ate_date,is_ms_shipped,ROW_NUMBER()over(partition by name,object_id,type,type_desc,create_date,is_ms_shipped order by name) as rk FROM temp_test ) t where t.rk > 1

SQL Server数据库中如何查询重复数据并处理

4、删除重复数据方法一:创建临时表-- 取去重的数据到备份表select distinct name,obje艘早祓胂ct_id,type,type_desc,create_date,is_ms_shipped into temp_test_new from temp_test-- 删除重复数据表drop table if exists temp_test-- 修改备份表名为原来表名EXEC sp_rename 'temp_test_new', 'temp_test'

SQL Server数据库中如何查询重复数据并处理

5、删除重复数据方法二:使用“with子句和ROW_NUMBER()函数”来实现删除完全重复的其他行WITH tab AS( SELECT ROW_NUMBER()over(partition by object_id order by object_id) as rk FROM temp_test)DELETE FROM tab WHERE rk>1

SQL Server数据库中如何查询重复数据并处理
© 手抄报圈