22,294
社区成员
发帖
与我相关
我的任务
分享create trigger tr_b
on b
for insert,update
as
update a set
price= t.up
,cost = t.up
from a,(
select no,itm
,sum(up) as up -- 或者max(up) as up
from inserted
group by no,itm
) as t
where a.no = t.no
and a.itm = t.itm
insert a(no,itm,price,cost)
select no,itm
,sum(up) as up -- 或者max(up) as up
,sum(up) as up -- 或者max(up) as up
from inserted i
where not exists (
select 1
from a
where a.no =i.no
and a.itm = i.itm
)
group by no,itm
go
--当然只是触发条件要你自己判断下选取,写法还是如下:
update a set price='up',cost='up' from a,inserted b
where a.no=b.no and a.itm=b.itm
--因为update实际相当于先delete再insert
--固从inserted表里取no,itm,其实从deleted表里取也可以
--第二个的话,update触发器要对应到更新具体哪个字段的,你要更新哪个字段然后触发?
--第一个insert触发器里这么写
update a set price='up',cost='up' from a,inserted b
where a.no=b.no and a.itm=b.itm