触发器更新导致错误

时间:2012-02-08 09:06:35

标签: sql sql-server triggers

我有一个触发器但是当它试图运行时我得到了

  

更新或删除的行值不会使行唯一...

我想在更新触发表中的col REDEEMED时更新表。

我需要更改触发器才能使其正常工作?

表GeneratedCouponCounter:

    Id int (primary key)
    CouponId int
    NrOfRedeemedCoupons int
    NrOfGeneratedCoupons int
    LastGenerated datetime
    LastRedeemed datetime
    CreatedOn datetime

触发:

CREATE TRIGGER trigger_update
ON GeneratedCoupon2
AFTER UPDATE
AS
    IF( UPDATE(REDEEMED))
    begin        
        update GeneratedCouponCounter
        SET NrOfRedeemedCoupons = NrOfRedeemedCoupons +1,[LastRedeemedOn] = getdate()
        where CouponId IN (SELECT CouponID from INSERTED)
    end

感谢名单!

/麦克

1 个答案:

答案 0 :(得分:0)

PRIMARY KEY表格上创建GeneratedCoupon2,当您更新“GeneratedCoupon2”表格中的多条记录时,更新的WHERE部分也会出现个案错误。将其更改为:
where CouponId IN (SELECT CouponID from INSERTED)

编辑: 更改了需要KEY的表名,以反映对问题的修复。

相关问题