34,590
社区成员
发帖
与我相关
我的任务
分享
--将中转表中已存在的数据插入到 临时表 #tmp2
select y.*
into #tmp2
from xxx as x inner join tmp as y
on x.a=y.a and x.b=y.b and x.c=y.c
--临时表加索引
create clustered index ix_#tmp2 on #tmp2(a,b,c)
--如果有已存在数据
if exists(select 1 from tmp2)
begin
--更新临时表中的 z
update tmp2 set z = ?
--更新原始表
update x
set c1=?,c2=? --列名自己写
from xxx as x inner join #tmp2 as y
on x.a=y.a and x.b=y.b and x.c=y.c
end
--找到在中转表中的,但不在临时表中的数据(也就是真正的新数据)
--插入到原始表
insert into xxx(a,b,c,...)
select a,b,c,...
from tmp as x left join #tmp2 as y
on x.a=y.a and x.b=y.b and x.c=y.c
where y.a is null