Library tutorials & articles
Using Triggers to track database action history
- Introduction
- Create tables
- Create triggers
Create tables
The table we'll examine was called Account_Info and contained the following structure:
| Table Name: Account_Info | |
| Column Name | Type/Size |
| account_id | varchar (25) |
| account_name | varchar(50) |
| account_balance | money |
| modifier | varchar(25) |
Notice the modifier column in the table. The modifier column records who added
the row to the Account_Info table, and is populated by the Web application,
which gets its value from the Session variable Request.ServerVariables("LOGON_USER"),
which, when using NT Challenge/Response, holds the name of the logged on user.
Whenever an Insert or Update is performed on the table, the modifier is tacked
on to remember who made the transaction. (Deletes are not considered in this
example since most records are not allowed for deletion in most State and Federal
systems; due to historical purposes, they are merely marked Inactive.) If you
like, go ahead and created a couple of ASP pages that will Insert and Update
the table.
While this technique will accurately record who inserted a record, or last updated
a record, it still leaves a lot to be desired. For example, if a particular
row is altered (updated) numerous times, the row will only contain one value
in the modifier column - who performed the last update. We may want to know
the complete history of changes. Also, we may want to have extra information,
such as when the record was initially created, and the date/time of each update
to the row.
In order to overcome these limitations, we'll create a new table whose sole
purpose will be to track the history for the Account_Info table. (Note that
for each table in your database design, you will need to add a new history table.)
The history table, History_Account_Info, has the following database
structure:
| Table Name: History_Account_Info | |
| Column Name | Type/Size |
| history_account_id | varchar (25) |
| history_account_name | varchar(50) |
| history_account_balance | money |
| modifier | varchar(25) |
| modified_date | datetime |
| action | varchar(15) |
Note that for each column in the Account_Info table there is
a corresponding column in the History_Account_Info table. Additionally,
the History_Account_Info table contains modifier_date
and action columns, to indicate when the change was made and what
change (and update or insert) was made.
Related articles
Related discussion
-
How to Databind PictureBox to database
by amazingpeople (0 replies)
-
Java Based Mobile Development Tool Now Supports Wireless PDA Access To SQL Server Databases
by mobiforms (0 replies)
-
Access SQL Server From Any Mobile PDA
by mobiforms (0 replies)
-
index out of range
by diwa.net (2 replies)
-
Index out of Range
by kausar4u (4 replies)
Related podcasts
-
Stack Overflow: Podcast #28
This is the twenty-eighth episode of the StackOverflow podcast, where Joel and Jeff discuss Windows Azure, SQL Server 2008 full text search, Bayesian filtering, porn detection, and project management — among other things. Jeff met the inestimable Joey DeVilla aka Accordion Guy...
Related jobs
-
Microsoft .Net Architect
in AMSTERDAM (€50K-€90K per annum) -
Ervaren SAP Basis Consultant
in Amsterdam (€50K-€90K per annum) -
Microsoft Dynamics CRM Consultant (Senior)
in Netherlands (€50K-€90K per annum) -
Microsoft Dynamics CRM Technical Consultant
in Netherlands (€50K-€90K per annum)
Events coming up
-
Mar
23
DevWeek 2009
London, United Kingdom
DevWeek is Europe’s leading independent conference for software developers, database professionals and IT architects, and features expert speakers on a wide range of topics, including .NET Framework 4.0, Silverlight 2, WCF 4.0, Visual Studio 2010, RESTful services, Windows Workflow, ASP.NET AJAX 4.0, SQL Server 2008, LINQ, C# 3, .NET Patterns, Ruby, and more.
Hai Tal
This Complete structure of the trigger and the table is fine...... this works fine and it will be good for one table.... but it will more fine and efficient if u embed all the operation info into one single trigger and run it up al at once.........
Oh, no.... I didn't say anything about using a cursor. There's no need for the RBAR (pronounced "ree-bar" and is a "Modenism" for "Row By Agonizing Row"). Do it all using proper set based techniques.
Hi Tal,
Jeff is right. Just declare a cursor that iterates over the inserted/deleted table. It should fetch at least your PKs, which you can use to retrieve the inserted/deleted lines - one after another.
However, your example is very sufficent and helped me a lot.
Thanks, Sven
Hi Tal,
You wrote "Note: A database trigger only fires once per statement; therefore, if you perform a mass update or mass delete then those instances will not be recorded for each record. Keep this in mind when you develop your own triggers. "
If you write your audit triggers to do an INSERT/SELECT from the INSERTED or DELETED table available in the trigger, then all records of a "mass update" or "mass delete" will be properly recorded in the audit table.
This thread is for discussions of Using Triggers to track database action history.