Wrox Programmer Forums
|
SQL Server 2000 General discussion of Microsoft SQL Server -- for topics that don't fit in one of the more specific SQL Server forums. version 2000 only. There's a new forum for SQL Server 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2000 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 10th, 2003, 11:33 AM
Authorized User
 
Join Date: Jun 2003
Posts: 34
Thanks: 0
Thanked 0 Times in 0 Posts
Default Table Audit

I have a need to audit a table and have the ability to report on the following;

Field <FieldName> has changed from <OriginalValue> to <NewValue> on <getdate()> by <SYSTEM_USER>



Thanks,
Jesse
__________________
Thanks,
Jesse
 
Old June 10th, 2003, 12:59 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 839
Thanks: 0
Thanked 1 Time in 1 Post
Default

There is no automatic easy way to do this, and there is no way I know to do this after the fact.

You can set up an UPDATE trigger on the table in question. In the trigger, you can insert into a journal/audit table information about those rows where a column in the Deleted pseudotable is not equal to the corresponding column value in the Inserted pseudotable. Note that your audit table should probably also have provision for recording rows in the original table which were inserted or deleted as well as changed.

Jeff Mason
Custom Apps, Inc.
www.custom-apps.com
 
Old July 16th, 2003, 01:44 PM
Authorized User
 
Join Date: Jun 2003
Posts: 34
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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
 
Old July 16th, 2003, 02:04 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 839
Thanks: 0
Thanked 1 Time in 1 Post
Default

Be careful.

The code you posted will not work if more than one author is deleted from the table in the same operation. That is, if I execute:
Code:
DELETE Authors WHERE State='MA';
This deletes all the authors whose state code is 'MA'. If more than one author lives in Massachusetts, you trigger will fail issuing an error message about a subquery returning more than one value...

Instead, your trigger's INSERT into your log table should read something like:
Code:
CREATE TRIGGER [LogTrig] ON [dbo].[authors] 
FOR INSERT, UPDATE, DELETE
AS
INSERT INTO LogTable 
(fromLine,RECID)
SELECT 'Changed State FROM ' + isnull(a.state,'<NULL>') + ' TO ' + isnull(rtrimd.state),'<NULL>') + ' BY ' + SYSTEM_USER + ' on ' + convert(char,getdate()),au_id
FROM deleted d
    INNER JOIN Authors a ON a.au_id=d.au_id;
handling the case where the 'deleted' pseudo-table which is visible in the trigger contains more than one row.

Jeff Mason
Custom Apps, Inc.
www.custom-apps.com





Similar Threads
Thread Thread Starter Forum Replies Last Post
Creating an Audit Trail Jade08 Pro VB 6 3 October 23rd, 2008 12:44 AM
Infopath Audit Trail sqaengineer Infopath 0 August 13th, 2007 10:10 AM
Audit in BizTalk steveculshaw Biztalk 0 June 28th, 2007 05:02 AM
audit logging ted BOOK: Access 2003 VBA Programmer's Reference 0 August 23rd, 2005 09:06 AM
Table Audit jesseleon Access VBA 2 June 10th, 2003 12:44 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.