我得到了以下代码(没有机会询问编写它的人)并且没有看到inner join on deleted
的需要。我错过了什么吗?
我想,删除表用在instead-of-delete triggers
中。在instead-of-inserted triggers
it's empty everytime中,是不是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;
答案 0 :(得分:0)
在更新触发器中,两个inserted
and deleted
都已填充。 inserted
包含新的行值,deleted
包含旧的行值,即执行UPDATE
语句之前的值。
这里似乎正在WHERE
子句中使用:
WHERE ( i."jobType" != 3 )
OR ( i."jobType" = 3 AND i."startTime" != d."startTime" );
因此,如果jobType
为3,则只有在startTime
发生更改后才会执行插入。
应该注意,只有在至少一个密钥的列不受更新的情况下,才能将inserted
和deleted
之间的行关联起来。在这里,id
似乎是关键。 希望 id
列不会更新,否则此触发器可能会在发生此类更新时出现异常。