62,046
社区成员
发帖
与我相关
我的任务
分享
--创建表结构
select 商品,库存数量 into b_copy from b where 1=2;
--合并结果放到中间表
insert into b_copy(商品,库存数量)
select t.商品,abs(sum(t.数量))
from (select a.商品,(a.盘点数量*-1) 数量 from a
union all select b.商品,b.库存数量
) t
group by t.商品
having sum(t.数量)<>0;
--按合并后的数量更新b表
update b set 库存数量=b_copy.库存数量
from b_copy
where b.商品=b_copy.商品;
--插入b表不存在的商品
insert into b(商品,库存数量)
select 商品,库存数量
from b_copy
where b_copy.库存数量<>0 and
not exists (select 1 from b_copy
where b_copy.商品=b.商品);
--删除中间表
drop table b_copy;