SQL中建2个表tb1,tb2。tb1和tb2的字段都为“编号,名称,规格”,tb2中的数据有与tb1重复的部分,怎么把重复的部分挑出,并且把不重复的接着tb1的编号的最后一个往下继续编号。就是查找“tb1.名称=tb2.名称 and tb1.规格=tb2.规格”的为重复的,怎么实现tb2中与tb1不重复的部分接着tb1的编号往下排?就是tb1中的编号最后一个是“009”的话,tb2的编号字段就接着“010”往下接。谢谢帮忙!

解决方案 »

  1.   

    if object_id('tb1') is not null
       drop table tb1
    if object_id('tb2') is not null
       drop table tb2
    go
    create table tb1(编号 int identity,名称 varchar(10),规格 varchar(10))
    insert into tb1
    select '名称1','规格1' union all
    select '名称2','规格1' union all
    select '名称3','规格1' union all
    select '名称4','规格1' union all
    select '名称5','规格2' union all
    select '名称6','规格2' union all
    select '名称7','规格2' union all
    select '名称8','规格2'
    create table tb2(编号 int identity,名称 varchar(10),规格 varchar(10))
    insert into tb2
    select '名称10','规格1' union all
    select '名称20','规格1' union all
    select '名称3','规格1' union all
    select '名称4','规格1' union all
    select '名称50','规格2' union all
    select '名称60','规格2' union all
    select '名称7','规格2' union all
    select '名称8','规格2'
    --select * from tb1
    --select * from tb2select a.编号,a.名称,a.规格 into # from tb1 a inner join tb2 b on a.名称=b.名称 and a.规格=b.规格insert into tb1
    select a.名称,a.规格 from tb2 a
    where a.编号 not in(select 编号 from #)drop table #
    select * from tb1
    接分
      

  2.   


    create table tb1(编号 varchar(10),名称 varchar(10),规格 varchar(10)) 
    insert into tb1 
    select '001','名称1','规格1' union all 
    select '002','名称2','规格1' union all 
    select '003','名称3','规格1' union all 
    select '004','名称4','规格1' union all 
    select '005','名称5','规格2' union all 
    select '006','名称6','规格2' union all 
    select '007','名称7','规格2' union all 
    select '008','名称8','规格2' 
    create table tb2(编号 varchar(10),名称 varchar(10),规格 varchar(10)) 
    insert into tb2 
    select '001','名称1','规格1' union all 
    select '002','名称2','规格1' union all 
    select '003','名称3','规格1' union all 
    select '004','名称4','规格1' union all 
    select '005','名称9','规格1' union all 
    select '006','名称10','规格2' union all 
    select '007','名称11','规格2' union all 
    select '008','名称12','规格2' 
    select id=identity(int,1,1),* into # from tb1
    insert into # select * from tb2 a
    where not exists(select 1 from tb1 where a.名称=名称 and a.规格=规格)
    insert into tb1 select right('000'+ltrim(id),3),名称,规格 from # a
    where not exists(select 1 from tb1 where a.名称=名称 and a.规格=规格)
    drop table #
    select * from tb1
      

  3.   

    看不懂你的意思,是查询,还是新加记录
    tb1编号    名称    规格    
    ----- ----- ----- 
    001   名称1   规格1
    002   名称2   规格2
    003   名称3   规格3
    004   名称4   规格4
    005   名称5   规格5
    006   名称6   规格6
    007   名称7   规格7
    008   名称8   规格8
    009   名称9   规格9
    tb2编号    名称    规格    
    ----- ----- ----- 
    001   名称1   规格1
    003   名称3   规格3
    004   名称4   规格4
    066   名66   规66
    008   名称8   规格8
    077   名77   规77
    sql查询: 
    select 
    right('00000'+
    convert(varchar(5),
    (
      select  count(*) + 1+ convert(integer,(select max(tb1.编号) from tb1 )) from tb2  
      where tb2.名称 + tb2.规格 not in (select 名称 + 规格 from tb1) 
          and tb2.编号 <a.编号
    ))
    ,5)  as '编号',
    名称,
    规格
    from tb2 a where a.名称 + a.规格 not in (select 名称 + 规格 from tb1)
    结果:
    编号         名称    规格    
    ---------- ----- ----- 
    00010      名66   规66
    00011      名77   规77
      

  4.   

    重复部分,你在后面加下union tb1
      

  5.   

    declare @maxnum numeric
    select @maxnum = max(编号) + 1 from tb1select identity(numeric,@maxnum,1) as 编号,名称,规格 from tb2 where tb2.名称 + tb2.规格 not in (select tb2.名称 + tb2.规格 from tb1)
      

  6.   

    Sorry,错了declare @maxnum numeric 
    select @maxnum = max(编号) + 1 from tb1 select identity(numeric,@maxnum,1) as 编号,名称,规格 from tb2 where tb2.名称 + tb2.规格 not in (select tb1.名称 + tb1.规格 from tb1)
      

  7.   

    into 到一个临时表名declare @maxnum numeric  
    select @maxnum = max(编号) + 1 from tb1  select identity(numeric,@maxnum,1) as 编号,名称,规格 into tb3  from tb2 where tb2.名称 + tb2.规格 not in (select tb1.名称 + tb1.规格 from tb1)
      

  8.   

    Create Table tb1
           (Number Nvarchar(5),--编号
            Name   Nvarchar(20),--名称
            Kind   Nvarchar(10)--类型
            )
    Create Table tb2
           (Number Nvarchar(5),
            Name   Nvarchar(20),
            Kind   Nvarchar(10)
            )
    Insert tb1 values('001','aa','C001')
    Insert tb1 values('002','bb','C002')
    Insert tb1 values('003','cc','C003')
    Insert tb2 values('001','aa','C001')
    Insert tb2 values('002','bb','C002')
    Insert tb2 values('003','cc','C003')
    Insert tb2 values('004','dd','C004')
    Insert tb2 values('005','ee','C005')
    Insert tb2 values('006','ff','C006')
    Insert tb2 values('007','gg','C007')
    Insert tb2 values('008','hh','C008')insert tb1
    select * from tb2
    where not exists (select* from tb1 
    where Name=tb2.Name and Kind=tb1.Kind)select * from tb1
    --Drop Table tb1
      

  9.   

    前提是tb1的编号是自增长的
    insert into tb1(名称,规格)
    Select tb2.名称,tb2.规格 From tb2 Where tb2.名称 not in (Select tb1.名称 From tb1)
      

  10.   

    --根据你的意思,是把重复的数据只需要一份(包括编号),不重复的挨着排下去,如果是这样的话,最简单的方法:(sql server2005)SELECT 编号,名称,规格 FROM (SELECT 编号,名称,规格 from tb1 WITH (NOLOCK)
    INTERSECT
    SELECT 编号,名称,规格 from tb2 WITH (NOLOCK)
    ) a
    UNION ALL
    SELECT 编号,名称,规格 FROM (SELECT 编号,名称,规格 from tb1 WITH (NOLOCK)
    EXCEPT
    SELECT 编号,名称,规格 from tb2 WITH (NOLOCK)
    ) b