select distinct * into #t from 表set xact_abort on
begin tran
delete from 表
insert 表 select * from #t
commit trandrop table #t

解决方案 »

  1.   

    delete Youtable where [id] not in (select max(id) from Youtable group by 重复的字段)
      

  2.   

    Select Distinct 列名 Form 数据库名……………………
      

  3.   

    --如果只是根据几个字段来确定重复---1.如果表中有主键,可以直接这样删除 delete 表
    from 表 a left join(
      select name,主键=min(主键) from 表 group by name  --假设以name字段判断是否重复
    )b on a.name=b.name and a.主键=b.主键
    where b.主键 is null
    -----------------------------------------------------------2.如果没有主键,建议增加一个标识字段来处理(这样才有效率)--添加标识字段
    alter table 表 add 标识字段 decimal(38,0) identity(1,1)
    go--删除处理
    delete 表
    from 表 a left join(
      select name,标识字段=min(标识字段) from 表 group by name --假设以name字段判断是否重复
    )b on a.标识字段=b.标识字段
    where b.标识字段 is null
    go--完成后删除标识字段(不过我建议保留标识字段,表中没主键,很多处理都比较麻烦)
    alter table 表 drop column 标识字段
    ------------------------------------------------------------3.如果你不愿意修改表结构,那就只好用临时表了(最低效的办法)--生成临时表
    select 标识字段=identity(decimal(38,0),1,1),* into #t from 表
    go--删除处理
    delete #t
    from 表 a left join(
      select name,标识字段=min(标识字段) from #t group by name
    )b on a.name=b.name and a.标识字段=b.标识字段
    where b.标识字段 is null
    go--删除处理完成后,删除临时表中的标识字段,方便数据导入
    alter table #t drop column 标识字段
    go--删除原表中的数据
    truncate table 表   --这样速度够快,如果不能用这句,改为: delete  表
    go--导回不重复的数据
    insert into 表 select * from #t
    go--删除处理的临时表
    drop table #t