1

i have simple question regarding Triggers in sql.

I am completely new and i do not know how to handle it.

I have one table myshift with shiftid, starttime stoptime and lastupdate.

create trigger ShiftTriggerr on myshift for update as 
if update(stoptime)
update myshift set lastupdated = getdate() 

what i want is when stoptime will update, the lastupdate field will update with getdate().

but when i run this it does not update one row but updated all rows. I do not know how to apply check on this trigger

4

2 回答 2

2

您需要使用INSERTED虚拟表:

CREATE TRIGGER dbo.ShiftTriggerr 
ON dbo.myshift AFTER UPDATE
AS
BEGIN
     SET NOCOUNT ON;
     IF UPDATE(stoptime)
     BEGIN
         UPDATE A
         SET lastupdated = getdate() 
         FROM dbo.myshift A
         INNER JOIN INSERTED B
             ON A.shiftid = B.shiftid
     END
END
于 2013-05-27T14:50:25.463 回答
0

myshift应该定义一个主键,如果是shiftId,那么,

  create trigger ShiftTriggerr on myshift for update as        
     update m
        set lastupdated = getdate() 
     From myshift m Join inserted i 
        on i.shiftId = m.shiftId 
于 2013-05-27T14:50:09.327 回答