Avatar of donhannam
donhannam
Flag for New Zealand asked on

SQL Trigger - update - add date to date field when status field changes to value 2

I want to set up a trigger on a table so that when the status field is changed to 2 (will be from either 0 or 1) that the trigger inserts the date the status was set to 2 in a seperate field say StatusUpdatedDate. I do not want this updated if another field on this record is update so carn't just check the field value = 2 and then update date with getdate.
Would like to read the status of the record before it is updated to compare. I can get the value of the inserted record but not sure how to get the value of the record before updated - I guess SQL deletes and inserts?.
Microsoft SQL Server

Avatar of undefined
Last Comment
donhannam

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Kevin Cross

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
donhannam

ASKER
Great thanks - both will work - I think I'll use the delete/insert comparison.
if deleted.status < 2 and inserted.status = 2
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23