2012-01-31 79 views
0

我有一个表叫卡:如何在列更新时触发时间戳设置?

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

由于默认情况下,启动设置为false。我想要做的就是当card更新激活设置为true时,第一次设置activationDate。

回答

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