【问题标题】:How do I trigger a timestamp to be set when a column is updated?如何在更新列时触发设置时间戳?
【发布时间】:2012-02-22 18:50:16
【问题描述】:

我有一张叫 Card 的桌子:

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

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

【问题讨论】:

    标签: sql sql-server-2005 triggers


    【解决方案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 
    

    【讨论】:

      猜你喜欢
      • 2014-09-30
      • 2021-02-25
      • 1970-01-01
      • 2014-04-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-30
      相关资源
      最近更新 更多