触发器 (Delete Update)

--delete触发器
IF(EXISTS(SELECT * FROM sysobjects WHERE name='T_PlanQtyDelete'))
DROP TRIGGER T_PlanQtyDelete
go

   Create trigger T_PlanQtyDelete
       On PlanQty
       for Delete
     As
        INSERT INTO dbo.PlanQtyHistory(qty, addDate, Productid)
        SELECT qty, addDate, Productid FROM deleted
go
 
--update触发器
IF(EXISTS(SELECT * FROM sysobjects WHERE name='T_PlanQtyUpdate'))
DROP TRIGGER T_PlanQtyUpdate
go

CREATE TRIGGER T_PlanQtyUpdate
    ON PlanQty FOR UPDATE
AS        
    IF UPDATE(qty)
        BEGIN
            INSERT INTO dbo.PlanQtyHistory(qty, addDate, Productid)        
            SELECT qty, addDate, Productid FROM deleted 
            --deleted  表示修改前的值   inserted 表示修改后的值
        END
    

deleted:删除之前会先把记录存在这张表里 

inserted:修改之前会先把记录写在这种表里

update 会先把记录写入deleted 然后再inserted

 

同时 truncate table PlanQty  不会触发  delet触发器 换成delete 

posted @ 2012-12-07 11:04  划破黑夜  阅读(5935)  评论(0编辑  收藏  举报