I agree with others - set the default value for GetDate () in the LastUpdate column, and then use the trigger to handle any updates.
Just something like this:
CREATE TRIGGER KeepUpdated on Profiles FOR UPDATE, INSERT AS UPDATE dbo.Profiles SET LastUpdate = GetDate() WHERE Username IN (SELECT Username FROM inserted)
If you want to truly introduce yourself, evaluate what has changed compared to what is in the database, and only change LastUpdate if there is a difference.
Consider this ...
7am . User jsmith is created with the last name "Smithe" (oops), LastUpdate defaults to 7 am
8am - "jsmith" email to say that his name is incorrect. You update immediately, so the last name is now Smith and (thanks to the trigger). LastUpdate shows 8am
2pm - Your current employee is finally bored with StumbleUpon and checks his email. He sees an earlier message from jsmith regarding a name change. It starts: UPDATE Profiles SET LastName = 'Smith' WHERE Username = 'jsmith', and then goes back to surfing on MySpace. The trigger doesn't care that the last name was already "Smith," so LastUpdate now shows 2pm.
If you simply blindly change LastUpdate whenever the update instruction is executed, it is TECHNICAL because the update has occurred, but it probably makes sense to actually compare the changes and act accordingly. So the Update 2pm Update statement will work, but LastUpdate will still show 8am.
- Kevin
Kevin Fairchild Aug 30 '08 at 16:02 2008-08-30 16:02
source share