temp表中有则更新 ,无则追加

解决方案 »

  1.   


    declare @tb table(jhh int,sbh varchar(50),dw varchar(50),mc varchar(50),nf varchar(40),yf int,ysl int)
    insert into @tb
    select  1,'A1001','101','报社','2008',1,80 union all
    select  2,'A1001','102','报社','2008',1,50 union all
    select  3,'A1003','104','电信','2008',1,70 union all
    select  4,'A1006','108','邮局','2008',1,60 union all
    select  5,'A1006','109','邮局','2008',2,90 union all
    select  6,'A1006','107','邮局','2008',2,10 
    select min(jhh)jhh,max(sbh)sbh,min(dw)dw,mc,nf,yf,sum(ysl)ysl from @tb group by mc,nf,yf/*
    1 A1001 101 报社 2008 1 130
    3 A1003 104 电信 2008 1 70
    4 A1006 108 邮局 2008 1 60
    5 A1006 107 邮局 2008 2 100
    */
      

  2.   

    如果你要有则更新,给你两个建议:
    1,可以将查询后的数据先插入临时表,如果TEMP中包含你临时表的记录,可以先删除,再全部插入
    2、可以通过UPDATE,及插入不存在的货号即可。因为你提供的还不能完整的写,比如,怎么判断存在与不存在。所以,你自己写了。
    下了