日期:2014-05-18 浏览次数:20366 次
CREATE TRIGGER tr_a_update ON [表A] FOR UPDATE AS BEGIN DECLARE @i INT,@sid INT SELECT @i=[quan],@sid=[sid] FROM DELETED SELECT @i=@i-[quan] FROM INSERTED UPDATE [表B] SET [quans]=[quan]-@i WHERE [sid]=@sid END
------解决方案--------------------
表 A,有字段 id,sid,quan
表 B,有字段 sid,quans
当更新A表的时候..例如 quan-1, update A set quan=quan-1 where sid=1
那么表B,quans-1,
如果 quan-2 那么表B quans-2
----------------------
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go
ALTER TRIGGER [UpdateTRIGGER]
ON [dbo].[PoliceDepartment]
FOR UPDATE
AS
BEGIN
UPDATE 表 B SET (表 B).quans =quan
FROM inserted a WHERE a.sid=表 B.sID
SET NOCOUNT ON;
-- Insert statements for trigger here
END
------解决方案--------------------
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go
create TRIGGER [UpdateTRIGGER]
ON [dbo].[PoliceDepartment]
FOR UPDATE
AS
BEGIN
UPDATE 表 B SET (表 B).quans =quan
FROM inserted a WHERE a.sid=表 B.sID
SET NOCOUNT ON;
-- Insert statements for trigger here
END
------解决方案--------------------
同意楼上
从DELETED取出原始值
从INSERTED取出现有值
两者之差就是新增的值
然后Update 表B