栗子说明:有两张表TransVouchs和so_sodetails(TransVouchs:so_sodetails=n:1),满足TransVouchs.cDefine25=so_sodetails.cSOCode和TransVouchs.cinvcode=so_sodetails.cinvcode时,更新so_sodetails。
具体如下:
1.当TransVouchs表插入一条记录时,so_sodetails更新iQuantity字段(加上增加记录中的iQuantity值) 2.当TransVouchs表删除一条记录时,so_sodetails更新iQuantity字段(减去删除记录中的iQuantity值) 3.当TransVouchs表update一条记录时: 如果更新字段比当前字段值大,则加上比当前值大的部分 如果更新字段比当前字段值小,则减去比当前值小的部分
------------------------------------------------------------------------------------------------------------------------------------------
TransVouchs:itvquantity,cinvcode,cDefine25
so_sodetails:cSOCode,cinvcode,iQuantity
1.当TransVouchs表插入一条记录时,so_sodetails更新iQuantity字段(加上增加记录中的iQuantity值)
create trigger tr_insert on TransVouchs for insert as declare @iQty decimal(30,10),@cinvcode1 nvarchar(60),@cDefine251 nvarchar(60) select @iQty=itvquantity,@cinvcode1=cinvcode,@cDefine251=cDefine25 from inserted; update so_sodetails set iQuantity=iQuantity+@iQty from so_sodetails where cSOCode=@cDefine251 and cinvcode=@cinvcode1
2.当TransVouchs表删除一条记录时,so_sodetails更新iQuantity字段(减去删除记录中的iQuantity值)
create trigger tr_delete on TransVouchs for delete as declare @iQty decimal(30,10),@cinvcode1 nvarchar(60),@cDefine251 nvarchar(60) select @iQty=itvquantity,@cinvcode1=cinvcode,@cDefine251=cDefine25 from deleted; update so_sodetails set iQuantity = iQuantity - @iQty from so_sodetails where cSOCode=@cDefine251 and cinvcode=@cinvcode1
3.当TransVouchs表update一条记录时:
如果更新字段比当前字段值大,则加上比当前值大的部分
如果更新字段比当前字段值小,则减去比当前值小的部分
create trigger tr_update on TransVouchs for update as declare @oldItvquantity decimal(30,10),@newItvquantity decimal(30,10),@temp decimal(30,10),@cinvcode1 nvarchar(60),@cDefine251 nvarchar(60) select @oldItvquantity=itvquantity,@cinvcode1=cinvcode,@cDefine251=cDefine25 from deleted; select @newItvquantity=itvquantity from inserted; set @temp= @newItvquantity - @oldItvquantity update so_sodetails set iQuantity=iQuantity+@temp from so_sodetails where cSOCode=@cDefine251 and cinvcode=@cinvcode1
三合一触发器!!!
create trigger tr_insert_delete_update on TransVouchs FOR INSERT,DELETE,UPDATE AS BEGIN IF EXISTS(SELECT 1 FROM deleted) AND EXISTS(SELECT 1 FROM inserted) BEGIN declare @oldItvquantity decimal(30,10),@newItvquantity decimal(30,10),@temp decimal(30,10),@cinvcode1 nvarchar(60),@cDefine251 nvarchar(60) select @oldItvquantity=itvquantity,@cinvcode1=cinvcode,@cDefine251=cDefine25 from deleted; select @newItvquantity=itvquantity from inserted; set @temp= @newItvquantity - @oldItvquantity update so_sodetails set iQuantity=iQuantity+@temp from so_sodetails where cSOCode=@cDefine251 and cinvcode=@cinvcode1 END ELSE IF EXISTS(SELECT 1 FROM deleted) AND NOT EXISTS(SELECT 1 FROM inserted) BEGIN declare @iQty decimal(30,10),@cinvcode111 nvarchar(60),@cDefine25111 nvarchar(60) select @iQty=itvquantity,@cinvcode111=cinvcode,@cDefine25111=cDefine25 from inserted; update so_sodetails set iQuantity=iQuantity+@iQty from so_sodetails where cSOCode=@cDefine25111 and cinvcode=@cinvcode111 END ELSE BEGIN declare @iQty22 decimal(30,10),@cinvcode122 nvarchar(60),@cDefine25122 nvarchar(60) select @iQty22=itvquantity,@cinvcode122=cinvcode,@cDefine25122=cDefine25 from deleted; update so_sodetails set iQuantity = iQuantity - @iQty22 from so_sodetails where cSOCode=@cDefine25122 and cinvcode=@cinvcode122 END END /* 插入操作(Insert):Inserted表有数据,Deleted表无数据 删除操作(Delete):Inserted表无数据,Deleted表有数据 更新操作(Update):Inserted表有数据(新数据),Deleted表有数据(旧数据) */