Created
April 20, 2011 09:42
-
-
Save max-mulawa/930854 to your computer and use it in GitHub Desktop.
DML Trigger Snippet 5
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CREATE TABLE dbo.CustomerNameAudit | |
( | |
CustomerName Varchar(10) NOT NULL, | |
ChangeDate DATETIME DEFAULT(GETDATE()) NOT NULL | |
) | |
GO | |
CREATE TRIGGER dbo.Customers_AfterUpdateInsertAudit | |
ON dbo.Customers | |
AFTER UPDATE, INSERT | |
AS | |
BEGIN | |
INSERT INTO CustomerNameAudit( CustomerName) | |
SELECT i.CustomerName | |
FROM inserted i | |
END | |
GO | |
INSERT INTO dbo.Customers(CustomerName) | |
VALUES('Maks Z.o.o') | |
INSERT INTO dbo.Customers(CustomerName) | |
VALUES('Asia S.A.') | |
GO | |
UPDATE Customers | |
SET CustomerName = 'Asia & Co.' | |
WHERE CustomerName = 'Asia S.A.' | |
GO | |
SELECT CustomerName, ChangeDate | |
FROM CustomerNameAudit | |
ORDER BY CustomerName | |
GO | |
DROP TRIGGER dbo.Customers_AfterUpdateInsertAudit; | |
GO |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment