I have an update trigger, part of which is below that is causing the problem.
if ( (select [Account] from inserted) != (select [Account] from deleted))
begin
UPDATE Customers SET active_changed = CONVERT(varchar(10),GetDate(),102)
FROM Customers ,INSERTED
WHERE Customers .ID = INSERTED.ID
end
If I do query like this
UPDATE Customers SET RepID= '111' where repID = '222'
this does not work because the clause in the Update trigger fetches multiple records and it can not be executed.But if I do this
UPDATE Customers SET RepID = '111' where ID = '12345'
It works because ID is unique and fetches only one record.
Does someone has a remedy for this and a better way to rewrite my trigger?
What I am doing in the trigger is, if a a particular field in the record [Active] is changed (example: record made active or inactive), I am recording that date and put it in one of the field [active_changed]. This is just for audit purpose.
Apologies, I explained a bit wrong before, now fixed.
Any advice
Based on the requirement “I would like to register the date at which, it was changed. Account can either be ‘active’ or ‘not-active’.” I think you want this :
This assumes the records ‘never’ get updated on the ID field. If you also want to catch the situation where an UPDATE can do that and you want such an operation also to set the active_changed field, you’ll need something like below :
Hope this helps.
PS: Why store the active_changed as a varchar instead of a datetime ?