如何去掉表中重复的数据行?

feiery 2008-04-10 12:44:14
我有一个表,结构如下:

Name ID Role
------ --- -----
zhang 23 developer
zhang 23 developer
Li 24 developer
Li 24 developer
Li 24 developer
wang 26 Manager

表在建的时候没有设primary key,所以表里有很多重复的数据行,现在要去掉重复的数据行,要怎么做呢?谁能给点建议?最好给个代码,谢过了,先!!
...全文
138 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
bqb 2008-04-10
  • 打赏
  • 举报
回复
select distinct * into tmp from tb 
delete from tb
sp_rename 'tmp','tb'
-狙击手- 2008-04-10
  • 打赏
  • 举报
回复
select distinct * into # from tb
truncate table tb
insert into tb
select * from #
drop table #
dawugui 2008-04-10
  • 打赏
  • 举报
回复
[Quote=引用楼主 feiery 的帖子:]
我有一个表,结构如下:

Name ID Role
------ --- -----
zhang 23 developer
zhang 23 developer
Li 24 developer
Li 24 developer
Li 24 developer
wang 26 Manager

表在建的时候没有设primary key,所以表里有很多重复的数据行,现在要去掉重复的数据行,要怎么做呢?谁能给点建议?最好给个代码,谢过了,先!!
[/Quote]
select distinct * into tmp from tb
delete from tb
insert into tb select * from tmp
drop table tmp
liangCK 2008-04-10
  • 打赏
  • 举报
回复
select distinct * into # from tb
truncate table tb
insert into tb
select * from #
drop table #
ojuju10 2008-04-10
  • 打赏
  • 举报
回复
select distinct Name , ID , Role from tab
wzy_love_sly 2008-04-10
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 bqb 的回复:]
SQL codeselectdistinct*intotmpfromtbdeletefromtb
sp_rename'tmp','tb'
[/Quote]
不错
wynlc 2008-04-10
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 liangCK 的回复:]
SQL codeselect distinct * into # from tb
truncate table tb
insert into tb
select * from #
drop table #
[/Quote]

学习

34,594

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧