Home Database Mysql Tutorial SQL Server中删除重复记录的SQL语句

SQL Server中删除重复记录的SQL语句

Jun 07, 2016 pm 05:49 PM

本文章来详细的给各位朋友来介绍SQL Server中删除重复记录的SQL语句方法总结,一般情况下,我们会有两种情况的删除操作,一种是完全重复的记录,所有字段都重复的情况,另一种是部分关键字段重复的记录,其他字段不重复,或重复但可以忽略。

方法:

 代码如下 复制代码
          
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
    (SELECT MAX(主键) FROM [dbo].[myTable] GROUP BY 列1, 列2, 列3)从 SQL Server 2005 以后,用 CTE:

WITH tmpOrderdTable
AS
(
  SELECT 
    GroupID = ROW_NUMBER() OVER (PARTITION BY 列1, 列2, 列3 ORDER BY 主键)
  FROM
    [dbo].[myTable]
)
 
DELETE FROM tmpOrderdTable WHERE GroupID > 1

为了提高效率可以先开启单人存取模式,删除完再恢复多人存取模式:

# 开启单人存取模式 

 代码如下 复制代码

USE [master] 
ALTER DATABASE [myDB] SET SINGLE_USER WITH ROLLBACK IMMEDIATE 
 
# 开启多人存取模式 

USE [master] 
ALTER DATABASE [myDB] SET MULTI_USER WITH ROLLBACK IMMEDIATERelated Posts

 


SQL存储过程删除

 代码如下 复制代码


declare @max integer,@id integer
declare cur_rows cursor local for select 主字段,count(*) from 表名 主字段 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

A:保留id最大的行,删除其它行  
方法1  

 代码如下 复制代码

delete   [user]   from   [user]   t    
  inner   join(select   name,max(id)   as   id   from   [user]   group   by   name)   a  
  on   t.name   =   a.name   and   t.id      a.id  


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  
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Reduce the use of MySQL memory in Docker Reduce the use of MySQL memory in Docker Mar 04, 2025 pm 03:52 PM

Reduce the use of MySQL memory in Docker

How do you alter a table in MySQL using the ALTER TABLE statement? How do you alter a table in MySQL using the ALTER TABLE statement? Mar 19, 2025 pm 03:51 PM

How do you alter a table in MySQL using the ALTER TABLE statement?

How to solve the problem of mysql cannot open shared library How to solve the problem of mysql cannot open shared library Mar 04, 2025 pm 04:01 PM

How to solve the problem of mysql cannot open shared library

What is SQLite? Comprehensive overview What is SQLite? Comprehensive overview Mar 04, 2025 pm 03:55 PM

What is SQLite? Comprehensive overview

Run MySQl in Linux (with/without podman container with phpmyadmin) Run MySQl in Linux (with/without podman container with phpmyadmin) Mar 04, 2025 pm 03:54 PM

Run MySQl in Linux (with/without podman container with phpmyadmin)

Running multiple MySQL versions on MacOS: A step-by-step guide Running multiple MySQL versions on MacOS: A step-by-step guide Mar 04, 2025 pm 03:49 PM

Running multiple MySQL versions on MacOS: A step-by-step guide

How do I secure MySQL against common vulnerabilities (SQL injection, brute-force attacks)? How do I secure MySQL against common vulnerabilities (SQL injection, brute-force attacks)? Mar 18, 2025 pm 12:00 PM

How do I secure MySQL against common vulnerabilities (SQL injection, brute-force attacks)?

What are some popular MySQL GUI tools (e.g., MySQL Workbench, phpMyAdmin)? What are some popular MySQL GUI tools (e.g., MySQL Workbench, phpMyAdmin)? Mar 21, 2025 pm 06:28 PM

What are some popular MySQL GUI tools (e.g., MySQL Workbench, phpMyAdmin)?

See all articles