方法:
代码如下 | 复制代码 |
select distinct * into #tmp from tablename drop table tablename select * into tablename from #tmp drop table # tmp |
常有时候遇到需要删除SQL Server中的重复记录,这里有一些常用的删除重复记录的SQL,
最常用的 T-SQL 语句:
代码如下 | 复制代码 |
DELETE FROM [dbo].[myTable] WHERE 主键 NOT IN WITH tmpOrderdTable |
为了提高效率可以先开启单人存取模式,删除完再恢复多人存取模式:
# 开启单人存取模式
USE [master] USE [master]
代码如下
复制代码
ALTER DATABASE [myDB] SET SINGLE_USER WITH ROLLBACK IMMEDIATE
# 开启多人存取模式
ALTER DATABASE [myDB] SET MULTI_USER WITH ROLLBACK IMMEDIATERelated Posts
SQL存储过程删除
代码如下 | 复制代码 |
|
A:保留id最大的行,删除其它行
方法1
代码如下 | 复制代码 |
delete [user] from [user] t |
B:保留id最小的行,删除其它行
方法1
代码如下 | 复制代码 |
delete [user] from [user] t inner join(select name,min(id) as id from [user] group by name) a on t.name = a.name and t.id <> a.id |