入库出库后库存自动更新的SQL触发器语句是什么?

2020-05-27 社会 115阅读
CREATE trigger [tri_ruku]
on [tb2] for insert,delete,update
as
begin
update tb1 set kucun=kucun-deleted.ruku from tb1,deleted
where tb1.cpid=deleted.cpid;
update tb1 set kucun=kucun+inserted.ruku from tb1,inserted
where tb1.cpid=inserted.cpid
end;
----------------------------------------------------
CREATE trigger [tri_chuku]
on [tb3] for insert,delete,update
as
begin
update tb1 set kucun=kucun+deleted.chuku from tb1,deleted
where tb1.cpid=deleted.cpid;
update tb1 set kucun=kucun-inserted.chuku from tb1,inserted
where tb1.cpid=inserted.cpid
end;
------------------------------------------------------------------
-------------------------------------------------------
Create TRIGGER [dbo].[tgname]
ON [dbo].[tb2]
AFTER INSERT,UPDATE,DELETE AS
BEGIN
update tb1 set kucun=(select sum(ruku) from tb2 where cpid=tb1.cpid)
END
库存=入库-出库,这个语句修改一下也没问题,但t2中每条记录的改动都将导致t1的数据全部修改一遍,效率不高。
前两个触发器只影响t1的相关数据。
声明:你问我答网所有作品(图文、音视频)均由用户自行上传分享,仅供网友学习交流。若您的权利被侵害,请联系fangmu6661024@163.com