I tried to implement triggers for filling audit-trail table on this way.
Everything works fine as long as I don't update the primary key field value.
When I try to update PK value, an error occures.
The code is the following:
CREATE TRIGGER NameOfTheTrigger
ON dbo.TableName FOR DELETE, INSERT, UPDATE
AS BEGIN
declare
@type varchar(10) ,
@UpdateDate datetime ,
@UserName varchar(128)
if exists (select * from inserted) and exists (select * from deleted)
select @type = 'UPDATE'
else if exists (select * from inserted)
select @type = 'INSERT'
else
select @type = 'DELETE'
select @UpdateDate = getdate() ,
@UserName = system_user
/* this code is repeting for every field in the table*/
if update (TableName) or @type = 'DELETE'
insert dbo.AUDIT_TRAIL (TableName, FieldName, OldValue, NewValue,
UpdateDate, UserName, type)
select 'TableName', convert(varchar(20), 'FieldName'),
convert(varchar(1000),d.FieldName), convert(varchar(1000),i.FieldName),
@UpdateDate, @UserName, @type
from inserted i
full outer join deleted d
on i.PrimaryKeyFieldName = d.PrimaryKeyFieldName
where (i.FieldName<> d.FieldName or (i.FieldName is null and d.FieldName is
not null) or (i.FieldName is not null and d.FieldName is null))
END
How to slve the problem with updated (changed) primary key values?
What is the typical code for audit-trail triggers?
Thanks. 2 2211
Hi
If your Primary Key is likely to change then you may want to create a
manufactured primary key that does not change.
Doing a lot of processing in your triggers may increate your transaction
times. To minimize this you can simply save copies of the inserted and
deleted tables and then do your processing later.
You should check @@ROWCOUNT as the first statement in your trigger to make
sure that some records have changed.
John
"Zlatko Matić" <zl***********@sb.t-com.hr> wrote in message
news:d1**********@ls219.htnet.hr... I tried to implement triggers for filling audit-trail table on this way. Everything works fine as long as I don't update the primary key field value. When I try to update PK value, an error occures. The code is the following:
CREATE TRIGGER NameOfTheTrigger ON dbo.TableName FOR DELETE, INSERT, UPDATE AS BEGIN declare @type varchar(10) , @UpdateDate datetime , @UserName varchar(128)
if exists (select * from inserted) and exists (select * from deleted) select @type = 'UPDATE' else if exists (select * from inserted) select @type = 'INSERT' else select @type = 'DELETE'
select @UpdateDate = getdate() , @UserName = system_user
/* this code is repeting for every field in the table*/ if update (TableName) or @type = 'DELETE' insert dbo.AUDIT_TRAIL (TableName, FieldName, OldValue, NewValue, UpdateDate, UserName, type) select 'TableName', convert(varchar(20), 'FieldName'), convert(varchar(1000),d.FieldName), convert(varchar(1000),i.FieldName), @UpdateDate, @UserName, @type from inserted i full outer join deleted d on i.PrimaryKeyFieldName = d.PrimaryKeyFieldName where (i.FieldName<> d.FieldName or (i.FieldName is null and d.FieldName is not null) or (i.FieldName is not null and d.FieldName is null))
END
How to slve the problem with updated (changed) primary key values? What is the typical code for audit-trail triggers?
Thanks.
Zlatko Matić (zl***********@sb.t-com.hr) writes: I tried to implement triggers for filling audit-trail table on this way. Everything works fine as long as I don't update the primary key field value. When I try to update PK value, an error occures.
And the error message is?
As you don't disclose the error message, I cannot really say what you
should do to fix it. However, handling changes of primary keys in a
trigger offers some special challenges. In fact, this is only possible
for single-row operations. If you update the PK value for, say, ten
rows, there is no way to relate the rows in the deleted and inserted
tables to each other. (Unless there is alternate key that is immutable.)
So when you have:
from inserted i full outer join deleted d on i.PrimaryKeyFieldName = d.PrimaryKeyFieldName
and ten rows has changed, this result set gives you 20 rows, effective
as there had been an insert of 10 rows and delete or 10 others.
Another note: my experience is that inserted/deleted are not very
performant, and joining them can be really expensive. For this reason
I routinely insert the tables into table variables that I call
@inserted/@deleted and work with the table variables instead.
--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se
Books Online for SQL Server SP3 at http://www.microsoft.com/sql/techinf...2000/books.asp This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Raphael Gluck |
last post by:
Hi,
Is it possible for one to program one's pages as such that when a database
table is updated over the web, via a form, that an e-mail confirmation is
sent to a specified address, notifying...
|
by: Keith |
last post by:
Hi
I am developing an ASP application which will interact with a SQL database.
A requirement of the application is that there is a full audit trail of any
modifications to data.
I am...
|
by: Zlatko Matić |
last post by:
Hello.
I tried to implement audit trail, by making an audit trail table with the
following fileds:
TableName,FieldName,OldValue,NewValue,UpdateDate,type,UserName.
Triggers on each table were...
|
by: Parag |
last post by:
Hello,
I have been assigned the task to design the audit trail for the ASP.NET web
application. I don't know what the best practices for such audit trails are.
Our application one dedicated user...
|
by: hary08 |
last post by:
I have a module copied ftom this site, here it is:
Option Compare Database
Option Explicit
Public Function AuditTrail()
On Error GoTo Err_Audit_Trail
'ACC2000: How to Create an Audit...
|
by: hary08 |
last post by:
im doing a database for Hospital Admission, I have a log in form which prompt user for a password. The source of log in is to look for the values in my Table tblEmployees and match user name and...
|
by: rockdc1981 |
last post by:
I dont it is possible to put this codes together.. I want to prompt the user to save the record and at the same time have a log of audit trail. the codes work out fine separately.
Code for Audit...
|
by: babamc4 |
last post by:
I have a main form (mainformlung) with 5 subforms (followupacute, followuplate, biochemresults, haemresults and pftresults). I have copied Allen Browne's Audit Trail code (thanks to Allen Browne)...
|
by: Emin |
last post by:
Dear Experts,
We need to design a database which has an audit trail for updates to a
certain set of tables. The two main approaches I've seen for this
include shadow tables (an extra table to...
|
by: linyimin |
last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
|
by: erikbower65 |
last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA:
1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
|
by: kcodez |
last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM)
The start time is equivalent to 19:00 (7PM) in Central...
|
by: DJRhino1175 |
last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this -
If...
|
by: Rina0 |
last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
|
by: lllomh |
last post by:
Define the method first
this.state = {
buttonBackgroundColor: 'green',
isBlinking: false, // A new status is added to identify whether the button is blinking or not
}
autoStart=()=>{
|
by: Mushico |
last post by:
How to calculate date of retirement from date of birth
|
by: DJRhino |
last post by:
Was curious if anyone else was having this same issue or not....
I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
| |