select distinct * into temp from yourtable
truncate table yourtable
insert into yourtable select * from temp
drop table temp
谢谢各位。那要删除字段1重复的记录,比如:
字段1 字段2 字段3
11 232 32
11 323 3
11 56 5
现只要一条记录
11 232 32
要怎样做呢?
加一个字段id
alter table tablename add id int identity(1,1)not null
go
select * from tablename where id in(select min(id) from tablename group by 字段1)
或临时表
select id=identity(int,1,1),* into temp from tablename
select * from temp where id in(select min(id) from temp group by 字段1)
drop table temp
這裡有兩種情況:
(1.) 對完全重復的記錄(即所有子段均重復)
select distinct *
into #temp
from tablename
GO
truncate table tablename
GO
select *
into tablename
from #temp
GO
drop table #temp
(2.)對部分關鍵字重復的記錄,如重復的字段為 name,address ;要求得到這兩個字段唯一的
結果集.
select identity(int,1,1) as id,*
into #temp
from tablename
GO
select min(id) as id
into #tempyh
from #temp
group by name,address
GO
select *
from #temp
where id IN ( select id from #tempyh )
GO