I’m creating a trigger on one table which should update date_modified column. Is below code the best approach to do this? Am I doing this right?
IF EXISTS(SELECT * FROM DELETED) --checking if this is an update, not insert
BEGIN
IF NOT(UPDATE(date_modified)) -- checking if desired column was not updated
BEGIN
DECLARE @updatedID int
SELECT @updatedID = ID FROM deleted -- fetching updated record ID
UPDATE table SET date_modified=GETDATE() WHERE ID=@updatedID -- updating desired column
END
You can simply use this instead of declaring variable