For me this audit trigger works fine for me. This requires an audit table as follows:
CREATE TABLE [dbo].[Audit]( [Type] [char](1) NULL, [TableName] [varchar](128) NULL, [PK] [varchar](1000) NULL, [FieldName] [varchar](128) NULL, [OldValue] [varchar](max) NULL, [NewValue] [varchar](max) NULL, [UpdateDate] [datetime] NULL, [UserName] [varchar](128) NULL ) ON [PRIMARY] GO
The audit trigger saves the type of transaction, the connected user, and old and new values. The only limitation is that the table to be checked must have a primary key.
I have to say that I found this on a blog that I read, but for life I donβt remember who it was - so I apologize for that.
You need to make sure that you change the table name at the start of the run.
CREATE TRIGGER [dbo].[TR_TableName_Audit] ON [dbo].[TableName] FOR INSERT, UPDATE, DELETE AS DECLARE @bit INT, @field INT, @maxfield INT, @char INT, @fieldname VARCHAR(128), @TableName VARCHAR(128), @PKCols VARCHAR(1000), @sql VARCHAR(2000), @UpdateDate VARCHAR(21), @UserName VARCHAR(128), @Type CHAR(1), @PKSelect VARCHAR(1000) --You will need to change @TableName to match the table to be audited SELECT @TableName = 'TableName' -- date and user SELECT @UserName = SYSTEM_USER , @UpdateDate = CONVERT(VARCHAR(8), GETDATE(), 112) + ' ' + CONVERT(VARCHAR(12), GETDATE(), 114) -- Action IF EXISTS (SELECT * FROM inserted) IF EXISTS (SELECT * FROM deleted) SELECT @Type = 'U' ELSE SELECT @Type = 'I' ELSE SELECT @Type = 'D' -- get list of columns SELECT * INTO