Important alert: (current site time 7/15/2013 7:54:42 PM EDT)
 

VB icon

Table audit log

Email
Submitted on: 7/11/2003 9:01:51 AM
By: Jesse Leon  
Level: Intermediate
User Rating: By 6 Users
Compatibility: SQL Server 2000
Views: 16878
(About the author)
 
     This shows 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.
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
--**************************************
-- Name: Table audit log
-- Description:This shows 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.
-- By: JesseLeon
--
--This code is copyrighted and has-- limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=721&lngWId=5--for details.--**************************************

/* 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


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Intermediate category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments
6/15/2004 8:25:36 AM

Jesse Leon, what advise would you give me to make 2 databases (with common tables) in different places, to be updated daily? I mean, a way to sinchronize data? I know how to work with SQL Server but i have no experience in this kind of database work. Would be nice to have a trigger like this to every table i want to update? Or should i insert a field ALT_DATE (Alteration Date) in which each field the user alter or insert would be recorded in a txt file as SQL Statement and then applied to SQL Server? Or even make this through my Delphi Application? I think would be nice to do this directly via SQL Server, and then send the .sql file by mail or even use the SQL Replication feature...

Well, im really confused on what option i should use...

Thanks in advance
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.