/ Published in: SQL
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
--Create audit fields ALTER TABLE [TABLE] ADD FirstSysDate DATETIME NULL, FirstUserID VARCHAR(20) NULL, LastSysDate DATETIME NULL, LastUserID VARCHAR(20) NULL --Create insert and update triggers CREATE TRIGGER [Table_Insert] ON [TABLE] AFTER INSERT AS BEGIN SET NOCOUNT ON; UPDATE [TABLE] SET FirstSysDate=GETDATE(), FirstUserID=SYSTEM_USER FROM inserted WHERE inserted.TableID=[TABLE].TableID END GO ----------------- CREATE TRIGGER [Table_Update] ON [TABLE] AFTER UPDATE AS BEGIN SET NOCOUNT ON; UPDATE [TABLE] SET LastSysDate=GETDATE(), LastUserID=SYSTEM_USER FROM inserted WHERE inserted.TableID=[TABLE].TableID END GO