关于触发器

前几天贾工给我说了说触发器,正好有个机会我就试了一下:

 

CREATE TRIGGER WhenChangeIsOver

ON dbo.Service

FOR UPDATE

AS

       BEGIN

              print '按时完成'

              UPDATE Service

                     SET IsCompleteByTime = '1'

              WHERE IsOver = '1' AND NeedCompleteDate >= OverDate AND

                     ServiceID IN (SELECT ServiceID FROM Inserted)

                    

              print '没有按时完成'

              UPDATE Service

                     SET IsCompleteByTime = '2'

              WHERE IsOver = '1' AND NeedCompleteDate < OverDate AND

                     ServiceID IN (SELECT ServiceID FROM Inserted)

       END

 

需要注意的两个表:DeletedInserted

Deleted 表用于存储 DELETE UPDATE 语句所影响的行的复本。在执行 DELETE UPDATE 语句时,行从触发器表中删除,并传输到 deleted 表中。Deleted 表和触发器表通常没有相同的行。

Inserted 表用于存储 INSERT UPDATE 语句所影响的行的副本。在一个插入或更新事务处理中,新建行被同时添加到 inserted 表和触发器表中。Inserted 表中的行是触发器表中新行的副本。

 

posted @ 2004-09-01 15:07  xiahaitao  阅读(514)  评论(0编辑  收藏  举报