delete 
select *
from  Table
group by 号码
Having Count(*)>1

解决方案 »

  1.   

    select distinct * into #a  from TB GROUP BY 号码,姓名,年龄
    HAVING count(*)>1
    delete tb from tb a,#a b where a.号码=b.号码 and a.姓名=.姓名 and a.年龄=b.年龄insert into tb (号码,姓名,年龄)
    select 号码,姓名,年龄 from #a
      

  2.   

    delete 
    select *
    from  Table
    group by 号码,姓名,年龄
    Having Count(*)>1
      

  3.   

    select id=identity(int,1,1),* into #t from TB
    select * from TB where id not in(select min(id) from #t group by 号码)
    drop table #tb
      

  4.   

    alter table t1
    add key_col int not identity
    delete from t1
    where exists(select * t1 as t2
    where t2.c1=t1.c1 and t2.key_col>t1.key_col)
    alter table t1 drop column key_col
      

  5.   

    或者用
    set nocount on
    declare @count int
    declare @c1 int
    declare tmpcursor cursor for
    select c1 count(*) from t1 
    group by c1 having count(*)>1
    open tmpcursor
    fetch next from tmpcursor into @c1,@count
    while @@fetch_status=0
    begin
    set @count=@count-1
    set rowcount @count
    delete from t1 where c1=@c1
    fetch next from tmpcursor into @c1,@count
    end
    close tmpcursor
    deallocate tmpcursor
    go
      

  6.   

    select distinct * into a  from A
    delete from A
    insert A
    select * from a
    这样也可以达到这个效果
      

  7.   

    怎样删除或压缩日志文件:数据库能正常工作时用backup log 数据库名 with truncate_only
    数据库不能正常工作时用backup log 数据库名 with no_log
    上面只是清空事务日志,
    然后用下面的语句将数据库文件压缩为1MB
    dbcc shrinkdatabase(数据库名,1)
      

  8.   

    分离数据库,把日志移走,再重新附加
    insert into 临时表 select distinct * from 表 delete from 表
      

  9.   

    --數據庫中有多條完全相同記錄(C)
    if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[temp3]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
    drop table [dbo].[temp3]create table temp3(號碼 int not null ,姓名 varchar(10) not null,年齡 int null)
    goinsert into temp3
    select 123,'張三',25 union all
    select 123,'張三',25 union all
    select 123,'張三',25 union all
    select 124,'李四',26 union all
    select 124,'李四',26 union all
    select 125,'王五',27 union all
    select 125,'王五',27 union all
    select 126,'李明',28 l
    goselect * from temp3
    /*
    號碼     姓名      年齡
    123 張三 25
    123 張三 25
    123 張三 25
    124 李四 26
    124 李四 26
    125 王五 27
    125 王五 27
    126 李明 28*/alter table temp3 add  www int not null identity(1,1)
    godelete temp3 from temp3 a left join
    (
    select max(www) as www from temp3 group by 號碼 
    ) b on a.www=b.www where b.www is nullalter table temp3 drop column www 
    goselect * from temp3
    /*
    號碼     姓名      年齡
    123 張三 25
    124 李四 26
    125 王五 27
    126 李明 28*/
      

  10.   

    --如果重復指編號,姓名,年齡完全相同,下列語句實現重復的記錄保留一條:
    --方法A:
    select * into #temp1 from temp1
    go
    truncate table temp1
    go
    insert into temp select * from #temp
    go
    drop table #temp
    go--方法B:
    if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[temp3]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
    drop table [dbo].[temp3]create table temp3(號碼 int not null ,姓名 varchar(10) not null,年齡 int null)
    goinsert into temp3
    select 123,'張三',25 union all
    select 123,'張三',25 union all
    select 123,'張三',29 union all
    select 124,'李四',26 union all
    select 124,'李四',26 union all
    select 125,'王五',27 union all
    select 125,'王五',27 union all
    select 126,'李明',28 l
    goselect * from temp3
    /*
    號碼     姓名      年齡
    123 張三 25
    123 張三 25
    123 張三 29
    124 李四 26
    124 李四 26
    125 王五 27
    125 王五 27
    126 李明 28*/alter table temp3 add  www int not null identity(1,1)
    godelete temp3 from temp3 a left join
    (
    select max(www) as www from temp3 group by 號碼,姓名,年齡 
    ) b on a.www=b.www where b.www is nullalter table temp3 drop column www 
    goselect * from temp3
    /*
    號碼     姓名      年齡
    123 張三 25
    123 張三 29
    124 李四 26
    125 王五 27
    126 李明 28*/
      

  11.   

    select distinct * into #a  from TB GROUP BY 号码,姓名,年龄
    HAVING count(*)>1
    delete tb from tb a,#a b where a.号码=b.号码 and a.姓名=.姓名 and a.年龄=b.年龄insert into tb (号码,姓名,年龄)
    select 号码,姓名,年龄 from #a
      

  12.   

    -----一种最笨的方法---------------
    1、首先将:原表oldtable插入一个自动ID
    select indetity(int,1,1) as ID
    into newtable from oldtable
    2、新建一临时表
    select * into #temp1 from newtable  a
    where ID=(select min(ID) from newtable where 号码=a.号码)
    3、删除id
    alter table #temp1 drop column ID
    4、将oldtable清空
    delete from oldtable
    5、将#temp1插入表oldtable
    select * into oldtable from #temp1
      

  13.   

    select indetity(int,1,1) as ID
    into newtable from oldtable看了一下错了改为:
    select identity(int,1,1) as ID,*
    into newtable from oldtable