After trial and error I have found how you can create a trigger on a table that will show what is happening in your tables. This shows that you can log the original value before change, the new value after change, who changed it and when. This sample is based on the authors table in the Pubs db.
/* This is the sample Log table*/
CREATE TABLE [LogTable] (
[LogID] [int] IDENTITY (1000, 1) NOT NULL ,
[FromLine] [char] (100) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
[RECID] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
CONSTRAINT [PK_LogTable] PRIMARY KEY CLUSTERED
(
[LogID]
) ON [PRIMARY]
) ON [PRIMARY]
GO
/* This is the sample TRIGGER TO be added to the authors TABLE IN the Pubs db*/
CREATE TRIGGER [LogTrig] ON [dbo].[authors]
FOR INSERT, UPDATE, DELETE
AS
DECLARE @Myvar char(50), @Myvar2 Char(50)
SET @myvar = (SELECT au_id FROM deleted)
SET @Myvar2 = (Select State FROM authors WHERE au_id = @myvar)
INSERT INTO LogTable
(fromLine,RECID)
SELECT 'Changed State FROM ' + isnull(state,'<NULL>') + ' TO ' + isnull(rtrim(@MyVar2),'<NULL>') + ' BY ' + SYSTEM_USER + ' on ' + convert(char,getdate()),au_id
FROM deleted
Thanks,
Jesse
|