先看merge,

不用merge时:

--更新
update TA
    set ColA=isnull((select top 1 Value from TB where TB.UserId=TA.UserId and TB.TypeName=@val),0)
    where DATEDIFF(day,[Date],@day)=0
--插入没有的数据
insert into TA 
    select newid(),UserId,@day,0,0,Value from TB 
    where not exists (select UserId from TA where TA.UserId=TB.UserId and DATEDIFF(day,[Date],@day)=0) 
        and TypeName=@val

用Merge:

merge TA as a
using (select * from TB where TypeName=@val) as b
on b.UserId=a.UserId
when matched then update set a.ColA=b.Value
when not matched then insert values(newid(),UserId,@day,0,0,Value)
when not matched by source then update set a.ColA=0;

 

相关文章:

  • 2022-12-23
  • 2021-04-28
  • 2021-12-30
  • 2021-12-20
  • 2021-11-14
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-12-18
  • 2021-06-20
  • 2021-05-16
  • 2022-02-13
  • 2022-01-17
相关资源
相似解决方案