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的相关数据。