2012-01-31 54 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