Skip to content

Instantly share code, notes, and snippets.

@sandeep540
Created June 9, 2020 12:37
Show Gist options
  • Save sandeep540/36709ce88d5ee60c70f9c83c8214d252 to your computer and use it in GitHub Desktop.
Save sandeep540/36709ce88d5ee60c70f9c83c8214d252 to your computer and use it in GitHub Desktop.
SQL Trigger
CREATE TRIGGER User_log_INSERT on dbo.Users
AFTER INSERT AS
BEGIN
INSERT INTO dbo.Users_log
( uuid, firstname, lastname, age, social, gender, action, changeuser, modifiedOn)
SELECT i.uuid, i.firstname, i.lastname, i.age, i.social, i.gender, 'INSERTED', SUSER_NAME(), GETDATE() FROM inserted i
END
CREATE TRIGGER User_log_UPDATE on dbo.Users
AFTER UPDATE AS
BEGIN
INSERT INTO dbo.Users_log
( uuid, firstname, lastname, age, social, gender, action, changeuser, modifiedOn)
SELECT i.uuid, i.firstname, i.lastname, i.age, i.social, i.gender, 'UPDATED', SUSER_NAME(), GETDATE() FROM inserted i
END
CREATE TRIGGER User_log_DELETE on dbo.Users
AFTER DELETE AS
BEGIN
INSERT INTO dbo.Users_log
( uuid, firstname, lastname, age, social, gender, action, changeuser, modifiedOn)
SELECT i.uuid, i.firstname, i.lastname, i.age, i.social, i.gender, 'UPDATED', SUSER_NAME(), GETDATE() FROM deleted i
END
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment