All articles| All Pictures| All Softwares| All Video| Go home page| Write articles| Upload pictures

Reading number is top 10 articles
url传递中文的方法_[Asp.Net教程]
使用.NET存储XML数据_[Asp.Net教程]
Asp.net中防刷新重复提交、防后退方法_[Asp.Net教程]
PHP截取UTF-8字符串,解决半字符问题_[PHP教程]
ASP.NET服务器控件PleaseWaitButton_[Asp.Net教程]
.NET程序调用SSIS中的DTS包_.net资料_编程技术
调用存储过程并且使用返回值的基本方法_[SQL,Server教程]
学习PHP动态网页技术收集的几个有用函数_php资料_编程技术
一个读取扩展名为xml的资源文件的方法_.net资料_编程技术
C#,asp.net操作文件_[Asp.Net教程]
Reading number is top 10 pictures
Household design comfortable contracted
赵惟依写真3
Ashlynn Brooke a group sexy photo2
Black and white also sexy--YanLiu2
超级大兔子
王艳写真温柔如水1
Sell the barbecue as says father du breul5
来几张有特色的图片
农夫山泉变身记
The money of more than 100 countries and regions11
Download software ranking
虚拟机5.5.3版
功夫熊猫2(上集)
好色的外科大夫
网页特效实例大全
Unix video tutorial1
Adobe Flash Player(IE) 10.0.32.18 浏览器专用的FLASH插件
Boxer's Top ten classic battle4
Unix video tutorial3
Unix video tutorial2
Unix video tutorial5
归海一刀 published in(发表于) 2014/2/3 6:46:58 Edit(编辑)
用SQL语句删除重复记录的四种方法_[SQL Server教程]

用SQL语句删除重复记录的四种方法_[SQL Server教程]

用SQL语句删除重复记录的四种方法_[SQL Server教程]

此文章用了四种方法教你如何用SQL语句删除重复记录。

问题:怎样把具有相同字段的纪录删除,只留下一条。

例如:表test里有id,name字段,如果有name相同的记录只留下一条,其余的删除。name的内容不定,相同的记录数不定。

方案1:

1、将重复的记录记入temp1表:


select [标志字段id],count(*) into temp1 from [表名]
group by [标志字段id]
having count(*)>1

2、将不重复的记录记入temp1表:


insert temp1
select [标志字段id],count(*) from [表名]
group by [标志字段id]
having count(*)=1

3、作一个包含所有不重复记录的表:


select * into temp2 from [表名]
where 标志字段id in(select 标志字段id from temp1)

4、删除重复表:delete [表名]


5、恢复表:


insert [表名]
select * from temp2

6、删除临时表:


drop table temp1
drop table temp2

方案2:


declare @max integer,@id integer
declare cur_rows cursor local for
select id,count(*) from 表名 group by id 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 = @id
fetch cur_rows into @id,@max
end
close cur_rows
set rowcount 0

注:set rowcount @max - 1 表示当前缓冲区只容纳@max-1条记录﹐如果有十条重复的﹐就刪除

10条,一定会留一条的。也可以写成delete from 表名。


方案3:


create table a_dist(id int,name varchar(20))
insert into a_dist values(1,'abc')
insert into a_dist values(1,'abc')
insert into a_dist values(1,'abc')
insert into a_dist values(1,'abc')
exec up_distinct 'a_dist','id'
select * from a_dist
create procedure up_distinct(@t_name varchar(30)
,@f_key varchar(30))
--f_key表示是分组字段﹐即主键字段
as
begin
declare @max integer,@id varchar(30) ,
@sql varchar(7999) ,@type integer
select @sql = 'declare cur_rows cursor
for select '+@f_key+' ,count(*) from '
+@t_name +' group by ' +@f_key +' having count(*) > 1'
exec(@sql)
open cur_rows
fetch cur_rows into @id,@max
while @@fetch_status=0
begin
select @max = @max -1
set rowcount @max
select @type = xtype from syscolumns
where id=object_id(@t_name) and name=@f_key
if @type=56
select @sql = 'delete from '+@t_name+'
where ' + @f_key+' = '+ @id
if @type=167
select @sql = 'delete from '+@t_name+'
where ' + @f_key+' = '+''''+ @id +''''
exec(@sql)
fetch cur_rows into @id,@max
end
close cur_rows
deallocate cur_rows
set rowcount 0
end
select * from systypes
select * from syscolumns where
id = object_id('a_dist')

方案4:

可以用IGNORE_DUP_KEY:


create table dup (id int identity not null,
name varchar(50)not null)
go
insert into dup(name) values ('abc')
insert into dup(name) values ('abc')
insert into dup(name) values ('abc')
insert into dup(name) values ('abc')
insert into dup(name) values ('abc')
insert into dup(name) values ('abc')
insert into dup(name) values ('abc')
insert into dup(name) values ('cdefg')
insert into dup(name) values ('xyz')
insert into dup(name) values ('xyz')
go
select *
from dup
go
create table tempdb..wk(id int not null,
name varchar(50)not null)
go
create unique index idx_remove_dup
on tempdb..wk(name)
with IGNORE_DUP_KEY
go
INSERT INTO tempdb..wk (id, name)
select id, name
from dup
go
select *
from tempdb..wk
go
delete from dup
go
set identity_insert dup on
INSERT INTO dup (id, name)
select id, name
from tempdb..wk
go
set identity_insert dup off
go
select *
from dup
go

注:这里delete原表,再加入不重复的值。也可以通过join只delete原表中重复的值。


来源:网络







添加到del.icio.us 添加到新浪ViVi 添加到百度搜藏 添加到POCO网摘 添加到天天网摘365Key 添加到和讯网摘 添加到天极网摘 添加到黑米书签 添加到QQ书签 添加到雅虎收藏 添加到奇客发现 diigo it 添加到饭否 添加到飞豆订阅 添加到抓虾收藏 添加到鲜果订阅 digg it 貼到funP 添加到有道阅读 Live Favorites 添加到Newsvine 打印本页 用Email发送本页 在Facebook上分享


Disclaimer Privacy Policy About us Site Map

If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)
Copyright ©2011-
uuhomepage.com, Inc. All rights reserved.