select 流水号, 货物, 规格, 计量单位, 批号,货物失效日期,数量,
       所属单位=(select 所属单位 from a where a.序号 = b.序号) ,
       所属供应商=(select 所属供应商 from a where a.序号 = b.序号)
into #t
from b
insert into c(流水号, 货物, 规格, 计量单位, 批号,货物失效日期,数量,所属单位,所属供应商)
select 流水号, 货物, 规格, 计量单位, 批号,货物失效日期,数量,所属单位,所属供应商
from #t as t1,c as t2
where t1.流水号!=t2.流水号 or t1.货物!=t2.货物 or....update c 
set 数量=数量+select isnull( (select sum(数量) from #t where 
                where t1.流水号=t2.流水号 and t1.货物=t2.货物... ))

解决方案 »

  1.   

    --处理的存储过程
    create proc p_process
    @序号 int
    as
    set xact_abort on
    begin tran 
    --先更新已经存在的
    update c set 数量=b.数量
    from a,b,c
    where a.序号=@序号 and b.序号=@序号 and a.序号=b.序号
    and a.所属单位=c.所属单位 and a.所属供应商=c.所属供应商
    and b.货物=c.货物 and b.批号=c.批号 --插入不存在的
    insert c(所属单位,所属供应商,货物,规格,计量单位,批号,货物失效日期,数量)
    select a.所属单位,a.所属供应商,b.货物,b.规格,b.计量单位,b.批号,b.货物失效日期,b.数量
    from a,b,c
    where a.序号=@序号 and b.序号=@序号 and a.序号=b.序号
    and not exists(
    select * from c 
    where a.所属单位=c.所属单位 and a.所属供应商=c.所属供应商
    and b.货物=c.货物 and b.批号=c.批号)
      

  2.   

    select 流水号, 所属单位=(select 所属单位 from a where a.序号 = b.序号) ,
           所属供应商=(select 所属供应商 from a where a.序号 = b.序号),货物, 规格, 计量单位, 批号,货物失效日期,数量
    into #table
    from b   ---------使用中间表过渡
    insert into c(流水号, 所属单位,所属供应商,货物, 规格, 计量单位, 批号,货物失效日期,数量)
    select 流水号, 所属单位,所属供应商,货物, 规格, 计量单位, 批号,货物失效日期,数量
    from #table as t1,c as t2
    where  t1.所属供应商<>t2.所属供应商 and t1.货物<>t2.货物 and  t1.计量单位<>t2.计量单位 and t1.批号<>t2.批号 
    update c 
    set 数量=数量+(select isnull(sum(数量),0) from #table a where  t1.所属供应商=t2.所属供应商 and t1.货物=t2.货物 and  t1.计量单位=t2.计量单位 and t1.批号=t2.批号)
      

  3.   

    谢谢各位,zjsen(星愿) zjcxc(邹建) davorsuker39(大狐狸) 
    一会儿揭贴