Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Update Trigger ChangeDate...how?

Author  Topic 

sth_Weird
Starting Member

38 Posts

Posted - 2009-10-12 : 09:27:53
hi,
In my table I have a column LastUpdate.
If the table changes, I want this column to automatically update and the value being set to the current datetime (getDate()).
My first thought was that this was a piece of cake, I created a trigger like this

...CREATE TRIGGER updateLastUpdate ON MyTable FOR UPDATE...
UPDATE MyTable SET LastUpdate = getDate() WHERE ID IN (SELECT ID FROM inserted)

But o_O, actually I'm starting a loop with that, don't I?
I mean by updating this column, I once again change the table, which means the trigger fires again...or am I wrong?
How can I update the column without running into this loop?
It has to work for mass updates, too, not only for one row (if there were only one-row-updates I could simply compare the LastUpdate values from inserted and updated and if they were equal then the change was caused by the trigger).
Can anyone give me some advice?
Hope you understand my problem, I'm not a native speaker...

sth_Weird

sth_Weird
Starting Member

38 Posts

Posted - 2009-10-12 : 09:30:21
uhm ok found the answer myself in the faq...
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2009-10-12 : 09:30:36
[code]
update t
set LastUpdate = getdate()
from MyTable t inner join inserted i on t.ID = i.ID
[/code]


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page
   

- Advertisement -