create trigger inserttb
on b
INSTEAD OF INSERT
as
begin
;with c1 as(
select 流水号,sum(拆分金额)as je from inserted group by 流水号
),c2 as(
select 流水号,sum(拆分金额)as je from B t where exists(select 1 from c1 where 流水号=t.流水号)
),c3 as(
select a.流水号,a.je+b.je as je from c1 a inner join c2 b on a.流水号=b.流水号
)
insert into B(流水号,拆分金额)
select i.* from inserted i inner join c3 c on i.流水号=c.流水号
where exists(select 1 from A where 流水号=i.流水号 and 金额<c.je)
end