更新列时如何触发设置时间戳?

时间:2012-01-31 00:47:53

标签: sql sql-server-2005 triggers

我有一张名为Card的桌子:

Card:
id (char(10))
points (int)
activated (bool)
activationDate(DateTime)

默认情况下,activate设置为false。我想要做的是在更新卡片激活后第一次设置activationDate设置为true。

1 个答案:

答案 0 :(得分:1)

可能是这样的

create trigger TriggerName
on Card
for update as
if update(activated )
     begin
         if exists (select activated from card
         where activated= false  && ID =SomeValue)
     begin
         rollback trigger with
         raiserror 24004 "Update failed "
     end
     else
     begin
         update Card
         set activationDate= GETDATE() Where ID=someValue
     end
    end