KirbyWallace
Programmer
Can anyone tell me if the following update trigger is the most efficient way to do what I'm doing? or is there a better way? I'm particularly concerned that the way I'm doing this requires me to make three selects to get three columns from the same table (deleted).
ALTER TRIGGER tbl_User_Update_SAC
ON tbl_User
AFTER UPDATE
AS
IF NOT UPDATE(Security_Access_Code)
RETURN
IF EXISTS (SELECT * FROM inserted a JOIN deleted b ON a.UserRecordID = b.UserRecordID
WHERE a.Security_Access_Code <> b.Security_Access_Code)
BEGIN
INSERT tbl_Audit_Log (UserSessionID, UserRecordID, Action_Type, Action_Desc)
VALUES (
Null, Null, 'Internal Audit',
'UserRecordID:' + cast((SELECT UserRecordID FROM inserted) as varchar(16)) +
' SAC CHANGED FROM: ' + (SELECT Security_Access_Code FROM deleted) +
' TO:' + (SELECT Security_Access_Code FROM inserted)
)
END
I've entered some blank lines for readability that are not there in the actual trigger code. This is working just fine, but I wonder if I'm going about it wrong or inefficiently.
Is there a better way? Or is this the standard way?
Thanks,
Kirby
ALTER TRIGGER tbl_User_Update_SAC
ON tbl_User
AFTER UPDATE
AS
IF NOT UPDATE(Security_Access_Code)
RETURN
IF EXISTS (SELECT * FROM inserted a JOIN deleted b ON a.UserRecordID = b.UserRecordID
WHERE a.Security_Access_Code <> b.Security_Access_Code)
BEGIN
INSERT tbl_Audit_Log (UserSessionID, UserRecordID, Action_Type, Action_Desc)
VALUES (
Null, Null, 'Internal Audit',
'UserRecordID:' + cast((SELECT UserRecordID FROM inserted) as varchar(16)) +
' SAC CHANGED FROM: ' + (SELECT Security_Access_Code FROM deleted) +
' TO:' + (SELECT Security_Access_Code FROM inserted)
)
END
I've entered some blank lines for readability that are not there in the actual trigger code. This is working just fine, but I wonder if I'm going about it wrong or inefficiently.
Is there a better way? Or is this the standard way?
Thanks,
Kirby