急!请问在一个表中有一些完全一样的记录,怎样只保留一条记录呢?

大虾好!
我用SQL Server数据库做了一个表,该表中有接近10000条记录,但是其中有很多都是完全一样的,我想把这些完全一样的数据只保留一条,其他全部从数据库中删掉,怎么办呀?
说明:该表没有关键字,有以下字段:name,province,mz,cc都是文字类型的。因此造成许多重复记录,请大虾帮忙了!
---------------------------------------------------------------

alter table 表 add newfield int identity(1,1)

delete 表
where newfield not in(
select min(newfield) from 表 group by 除newfield外的所有字段
)

alter table 表 drop column newfield
---------------------------------------------------------------

select distinct * into #temp from 表
truncate table 表
insert 表 select * from #temp
drop table #temp
---------------------------------------------------------------

SELECT DISTINCT * INTO # FROM TABLENAME
TRUNCATE TABLE TABLENAME
INSERT INTO TABLENAME
SELECT * FROM #
---------------------------------------------------------------

就是建一个各个字段一模一样的表,把重复的记录拷到那个临时表中,把原表中的重复记录删掉,用distict可以把一模一样多条的记录变成一条记录,然后拷回去

Published At
Categories with 数据库类
Tagged with
comments powered by Disqus