触发阻止列更新

时间:2014-04-14 23:09:28

标签: sql sql-server

我创建了一个触发器,如下所示:

CREATE TRIGGER InsertUpdateATLastViewedMatch
     ON  dbo.AT_LastViewedMatch
     FOR INSERT,UPDATE
AS 
BEGIN
  SET NOCOUNT ON;

    -- update statements for trigger here

    declare @id1 int, @matchFK int;

    select @id1 = i.id from inserted i;
    select @matchFK=i.AT_MatchFk from inserted i;
    update dbo.AT_LastViewedMatch set matchId = @matchFK where id = @id1;

END
GO

虽然这个触发器有效但我无法直接从SQLManager更新matchId列的值:

如果我执行

UPDATE AT_LastViewedMatch set matchId='1179619' where id=5762

例如,我仍然看到matchId的旧值。我认为这个问题与上面的触发器有关(因为如果我放下触发器,它就有效)。有没有办法解决这个问题?

1 个答案:

答案 0 :(得分:0)

从下面的触发器定义中,您已为insert and update

创建了它
CREATE TRIGGER InsertUpdateATLastViewedMatch
     ON  dbo.AT_LastViewedMatch
     FOR INSERT,UPDATE <-- Here

基本上,无论何时从SSMS运行更新语句

UPDATE AT_LastViewedMatch set matchId='1179619' where id=5762

您的触发器执行以下行并将其设置为旧值

update dbo.AT_LastViewedMatch set matchId = @matchFK where id = @id1;

您需要从触发器定义中删除update

CREATE TRIGGER InsertUpdateATLastViewedMatch
     ON  dbo.AT_LastViewedMatch
     FOR INSERT