if exists( select 单号 from a where 单号 in (select 单号 from b )) update b set b.总数量=a.tmpsum from b,(select 单号,sum(数量) as tmpsum from a group by 单号) as a where b.单号=a.单号 else insert b select b.单号,b.总数量 from b,(select 单号,sum(数量) as tmpsum from a group by 单号) as a where b.单号=a.单号
Group by 单号
update b set b.总数量=a.tmpsum
from b,(select 单号,sum(数量) as tmpsum from a group by 单号) as a
where b.单号=a.单号
else
insert b
select b.单号,b.总数量
from b,(select 单号,sum(数量) as tmpsum from a group by 单号) as a
where b.单号=a.单号