creat table test
(
 name char(10)
)
insert into test select 'a'
insert into test select 'a'
insert into test select 'b'
insert into test select 'b'
insert into test select 'c'
insert into test select 'c'
insert into test select 'd'
insert into test select 'd'
drop table test
/*
要删除重复的a,b,c,d记录,最后的结果集是这样:
a
b
c
d
*/

解决方案 »

  1.   


    create table test 

    name char(10) 

    insert into test select 'a' 
    insert into test select 'a' 
    insert into test select 'b' 
    insert into test select 'b' 
    insert into test select 'c' 
    insert into test select 'c' 
    insert into test select 'd' 
    insert into test select 'd' --操作
    select distinct * into # from test
    delete test
    insert into test select * from #
    drop table #--查看
    select * from test
      

  2.   

    早上不是有一贴吗:SELECT DISTINCT * INTO # FROM tb_name;TRUNCATE TABLE tb_name;INSERT tb_name
      SELECT * FROM #;DROP TABLE #;------稍微有点差别,这个是有不是仅仅只利用本表一个表,仅参考
      

  3.   

    create table test 

    aname varchar(10) 

    insert into test select 'a' 
    insert into test select 'a' 
    insert into test select 'b' 
    insert into test select 'b' 
    insert into test select 'c' 
    insert into test select 'c' 
    insert into test select 'd' 
    insert into test select 'd' 1、这方法太多了吧
      辅助表最很容易的select distinct * into # from test
    delete test
    insert into test select * from #
    drop table #2、也可以添加一标志列,通过标志列来操作,再删除标志列
       这里不说了,论坛上很多啊!3、如果要求不能用辅助表或修改表结构,那就用循环删除吧set rowcount 1 
    select 1
    while @@rowcount=1 
      delete  a from test a where (select count(1) from test where aname=a.aname)>1
    set rowcount 0
    --查看
    select * from test
    /*
    aname      
    ---------- 
    a
    b
    c
    d(所影响的行数为 4 行)
    */