0

我得到了以下代码(没有机会询问编写它的人)并且看不到inner join on deleted. 我错过了什么吗?我想,deleted-table 用于instead-of-delete triggers. instead-of-inserted triggers 里面每次都是空的,不是instead-of-update triggers吗?

CREATE TRIGGER "someTrigger" ON "dbo"."someView"
INSTEAD OF UPDATE
AS
BEGIN           
    INSERT INTO "someOtherTable"
    ( 
        "id"
        , "startTime"
        , "endTime"
        , "duration"
        , "resourceId"
        , "resourceLocked"
        , "timeLocked"
        , "groupPrefix"
    )
    SELECT 
        "id"                = i."id"
        , "startTime"       = i."startTime"
        , "endTime"         = i."endTime"
        , "duration"        = ISNULL( i."duration", 0 )
        , "resourceId"      = i."resourceId"
        , "resourceLocked"  = ISNULL( i."resourceLocked", 0 )
        , "timeLocked"      = ISNULL( i."timeLocked", 0 )
        , "groupPrefix"     = N'gp'
    FROM inserted AS i
    INNER JOIN deleted AS d
        ON d."id" = i."id"
    WHERE ( i."jobType" != 3 )
       OR ( i."jobType"  = 3 AND i."startTime" != d."startTime" );

END;
4

1 回答 1

0

在更新触发器中,inserteddeleted都被填充。inserted包含新行值,deleted包含旧行值,即UPDATE执行语句之前的值。

在这里,它似乎被用于WHERE子句:

WHERE ( i."jobType" != 3 )
   OR ( i."jobType"  = 3 AND i."startTime" != d."startTime" );

因此,如果是 3,我们仅在已更改jobType时才进行插入。startTime


应该注意的是,只有在至少一个键的列不受更新的情况下,才可能关联inserted和之间的行。deleted在这里,id似乎是一把钥匙。希望id列不会更新,否则当发生此类更新时,此触发器可能会出现异常。

于 2013-09-06T07:10:39.020 回答