I have a table for example called ServiceCase, it has a closedDate. Meaning if the case status changed to something means end of the case, then it should mark a closedDate. Otherwise, if it is reopened, then it should mark the ClosedDate to nullSo we have a trigger for the ServiceCase table. It does this:CREATE TRIGGER [dbo].[trgServiceCase_AfterIU]ON [dbo].[ServiceCase] AFTER INSERT, UPDATE AS BEGINIF @@ROWCOUNT = 0 RETURNSET NOCOUNT ON; -- Check the new statuses -- -- Set not-End Status updates closed status to null is not null UPDATE dbo.ServiceCase SET ClosedDate = NULL FROM ServiceCase TC INNER JOIN Inserted i ON I.ID = TC.ID INNER JOIN [dbo].[ServiceStatus] TS ON TS.ID = I.ServiceStatusID AND TS.IsEndState = 0 WHERE TC.ClosedDate IS NOT NULL -- -- Set End Status updates closed status to today is null UPDATE dbo.ServiceCase SET ClosedDate = GETDATE() FROM ServiceCase TC INNER JOIN Inserted i ON I.ID = TC.ID INNER JOIN [dbo].[ServiceStatus] TS ON TS.ID = I.ServiceStatusID AND TS.IsEndState = 1 WHERE TC.ClosedDate IS NULLEND;GOI kind of don't like the trigger much, if it is for one time update , it is fine.But the serviceCase table is a table frequently updated, the above statement will be triggered each time that does an update.Any suggestions to improve this?Thanks
↧