I have the following trigger:
CREATE TRIGGER Users_Delete
ON Users
AFTER DELETE
AS
BEGIN
SET NOCOUNT ON;
-- Patients
UPDATE Patients SET ModifiedByID=NULL WHERE ModifiedByID=ID;
UPDATE Patients SET CreatedByID=NULL WHERE CreatedByID=ID;
UPDATE Patients SET DeletedByID=NULL WHERE DeletedByID=ID;
END
I was wondering if there's a way to "combine" those three UPDATE statements into something that would be like the following:
UPDATE Patients SET
(ModifiedByID=NULL WHERE ModifiedByID=ID) OR
(CreatedByID=NULL WHERE CreatedByID=ID) OR
(DeletedByID=NULL WHERE DeletedByID=ID);
I'd really like to have only one statement to increase performance.
The reason I'm using the trigger instead on ON DELETE
for the FOREIGN KEY
is because I'm getting the error that having more than one ON DELETE
causes the following error:
Introducing FOREIGN KEY constraint 'FK_Patients_Users_Deleted' on table 'Patients' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.
EDIT: would it be good to have indexes on all of the ModifiedByID, CreatedByID, DeletedByID columns? Deleting a User will of course be rare, so is it worth adding indexes to 3 columns?