导表的时候,假设表A跟B,表A往B里导,但是表A里有一列中有重复项,这列在B表里为主键,不能重复,怎么着才能找到A里面的所有重复项,然后区分开,譬如某一项重复了4回,分别给这一4条记录前面加上A,B,C,D,SQL语句怎么写?? 我要具体的步骤 要在查询分析器里写的语句

解决方案 »

  1.   

    -- 导入不重复的记录
    insert into tbB
    select *
    from tbA a
    where not exists (select 1 from tbA b where b.thefield=a.thefield and b.id!=a.id)-- 重复记录导入临时表
    select 
       ...
      ,thefield = thefield + char(65+(select count(1) from tbA b0 where b0.thefield=a.thefield and b0.id<a.id)) 
    -- 序号 A,B,C..
      ,...
    into #tmp_tab
    from tbA a
    where exists (select 1 from tbA b where b.thefield=a.thefield and b.id!=a.id)
      

  2.   


    '借楼上思路,,'
    create table ax (a int  PRIMARY KEY , b int, c int)
    create table bx (a int, b int, c int)insert into bx values (11,12,13)
    insert into bx values (11,15,16)
    insert into bx values (11,16,16)
    insert into bx values (12,15,16)
    insert into bx values (12,16,16)
    insert into bx values (13,15,16)select   convert(varchar(20),a)+char(64+(select top 1  count(1) from bx  where t.a=a and t.b>=b ))a ,b,c
     from bx  t  where a not in (select a from bx group by a having(count(*))=1) 
    /*
    11A 12 13
    11B 15 16
    11C 16 16
    12A 15 16
    12B 16 16*/
      

  3.   

    表A插入主键 id int identity(1,1)
    select (select count(id) from 表A as a where b.a=a.a and a.id>=b.id),* from 表A as b
      

  4.   


    insert into b
    select a.* from  a
    where not exists(select 1 from b where a.id=b.id)
      

  5.   

    insert into b 
    select a.* from  a 
    where not exists(select 1 from b where a.id=b.id)
    这个不行,我适过了.有出错信息.违反了主码约束