473,561 Members | 3,153 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Trigger-Happy

I am trying to create a very minimal auditing system for a series of
databases.

I am in the process of writing Update triggers for 5 Tablse. I will
write a trigger for each table-the trigger's function will be to
INSERT a row into my MasterChanges table everytime ANY data is changed
in each of the 5 tables.
I have set up MasterChanges to capture the following:

what Table the change was made in
what Field the change was made in
what the Field was changed to
when this change occured.

I have used the Columns_Update( ) function to enable the trigger to
survey all 22 fields in the first table (my first attempt at this). I
can hardcode in the table's name, as there will be a separate trigger
for each table.
The problem is how to identify the NAME of the COLUMN that was updated
and have the trigger write that NAME into MasterChanges, as well as
the new value of that column.
My first thought is a very unacceptable hardcoding of multiple IF
STATEMENTS for each of the 22 fields.

ie:

IF(colA) then...elseif(c olB) then...elseif(c olC) then...etc.

There must be a better way to do it

I assume this would be done using Columns_Update( ) in some way, but
cannot figure it out.
Can anyone help?

Thanks,

Trevor Fairchild
Jul 20 '05 #1
2 4192
Trevor,

We've implemented something like what you're talking about, and
believe me, knowing what changes were made and who made them is a BIG
benefit. You can undo changes months later if you need to.

By no means am I an expert so don't automatically take this to be the
best way. It's just what we've come up with.

We initially had update triggers that checked the Deleted value
against the Inserted value for each column and if they didn't match,
then we added a record to the trace table. The IF logic (if
Update(ColumnA) , If Update(ColumnB) , etc...) in the trigger slowed
things down too much. We now have intermediate tables which are
basically duplicates of the base tables we want to trace, with a few
extra fields thrown in. In our Update trigger we don't check anything,
we just copy both the Inserted row and the Deleted row into the
intermediate table. Then in a job that runs once a night, we check
each set of Inserted and Deleted values in the intermediate table, and
if they don't match, we throw this information into the trace table.
This keeps the trigger processing in the base table to a minimum.

There are some other things that we do to ease the maintenance of
keeping these trace tables, but this is the basic structure of how
we're handling it, and right now it seems to work pretty good for us.

Hope this helps,

Gary

tr*****@e-crime.on.ca (Trevor Fairchild) wrote in message news:<5b******* *************** ****@posting.go ogle.com>...
I am trying to create a very minimal auditing system for a series of
databases.

I am in the process of writing Update triggers for 5 Tablse. I will
write a trigger for each table-the trigger's function will be to
INSERT a row into my MasterChanges table everytime ANY data is changed
in each of the 5 tables.
I have set up MasterChanges to capture the following:

what Table the change was made in
what Field the change was made in
what the Field was changed to
when this change occured.

I have used the Columns_Update( ) function to enable the trigger to
survey all 22 fields in the first table (my first attempt at this). I
can hardcode in the table's name, as there will be a separate trigger
for each table.
The problem is how to identify the NAME of the COLUMN that was updated
and have the trigger write that NAME into MasterChanges, as well as
the new value of that column.
My first thought is a very unacceptable hardcoding of multiple IF
STATEMENTS for each of the 22 fields.

ie:

IF(colA) then...elseif(c olB) then...elseif(c olC) then...etc.

There must be a better way to do it

I assume this would be done using Columns_Update( ) in some way, but
cannot figure it out.
Can anyone help?

Thanks,

Trevor Fairchild

Jul 20 '05 #2
One other thing I forgot to mention, We get the field name by using
the syscolumns table. This will give a list of the columns for any
table in the database. The new value comes from the triggers Inserted
table, and the old value comes from the triggers Deleted table

Gary

tr*****@e-crime.on.ca (Trevor Fairchild) wrote in message news:<5b******* *************** ****@posting.go ogle.com>...
I am trying to create a very minimal auditing system for a series of
databases.

I am in the process of writing Update triggers for 5 Tablse. I will
write a trigger for each table-the trigger's function will be to
INSERT a row into my MasterChanges table everytime ANY data is changed
in each of the 5 tables.
I have set up MasterChanges to capture the following:

what Table the change was made in
what Field the change was made in
what the Field was changed to
when this change occured.

I have used the Columns_Update( ) function to enable the trigger to
survey all 22 fields in the first table (my first attempt at this). I
can hardcode in the table's name, as there will be a separate trigger
for each table.
The problem is how to identify the NAME of the COLUMN that was updated
and have the trigger write that NAME into MasterChanges, as well as
the new value of that column.
My first thought is a very unacceptable hardcoding of multiple IF
STATEMENTS for each of the 22 fields.

ie:

IF(colA) then...elseif(c olB) then...elseif(c olC) then...etc.

There must be a better way to do it

I assume this would be done using Columns_Update( ) in some way, but
cannot figure it out.
Can anyone help?

Thanks,

Trevor Fairchild

Jul 20 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
1993
by: Matik | last post by:
Hello to all, I have a small question. I call the SP outer the DB. The procedure deletes some record in table T1. The table T1 has a trigger after delete. This is very importand for me, that the SP will be finished ASAP, that's why, I do not want, and I do not need to wait for a trigger.
6
6536
by: Scott CM | last post by:
I have a multi-part question regarding trigger performance. First of all, is there performance gain from issuing the following within a trigger: SELECT PrimaryKeyColumn FROM INSERTED opposed to: SELECT * FROM INSERTED
9
3449
by: Martin | last post by:
Hello, I'm new with triggers and I can not find any good example on how to do the following: I have two tables WO and PM with the following fields: WO.WONUM, VARCHAR(10) WO.PMNUM, VARCHAR(10) WO.PROBLEMCODE, VARCHAR(8)
6
7126
by: Mary | last post by:
We are developing a DB2 V7 z/OS application which uses a "trigger" table containing numerous triggers - each of which is activated by an UPDATE to a different column of this "trigger" table. When the triggers are fired, various other operations are performed on other tables in the database. The triggers are not created on these other...
0
7131
by: Dave Sisk | last post by:
I've created a system or external trigger on an AS/400 file a.k.a DB2 table. (Note this is an external trigger defined with the ADDPFTRG CL command, not a SQL trigger defined with the CREATE TRIGGER statement.) I've also defined a SQL stored proc, and the trigger is set to call this SP. I've posted the simplified source below. I can...
6
9221
by: JohnO | last post by:
Hi Folks, I have an update trigger that fails (it inserts an audit table record) in some circumstances. This is causing the triggering transaction to fail and roll back. Is there any way to prevent this? If the trigger fails I still want the triggering transaction to continue. Cheers, JohnO
0
2461
by: JohnO | last post by:
Thanks to Serge and MarkB for recent tips and suggestions. Ive rolled together a few stored procedures to assist with creating audit triggers automagically. Hope someone finds this as useful as I've found it educational. Note: - I build this for use in a JDEdwards OneWorld environment. I'm not sure how generic others find it but it...
1
2199
by: deepdata | last post by:
Hi, I am creating a trigger in DB2 express version. When i use the following syntax to create trigger CREATE TRIGGER USER_PK_TRIGGER BEFORE INSERT On users REFERENCING NEW As N FOR EACH ROW
9
9299
by: Ots | last post by:
I'm using SQL 2000, which is integrated with a VB.NET 2003 app. I have an Audit trigger that logs changes to tables. I want to apply this trigger to many different tables. It's the same trigger, with the exception of the table name. I could manually change the table name in the trigger and create it, over and over, but I'd like to automate...
11
7851
by: tracy | last post by:
Hi, I really need help. I run this script and error message appeal as below: drop trigger log_errors_trig; drop trigger log_errors_trig ERROR at line 1: ORA04080: trigger 'LOG_ERRORS-TRIG' does not exist drop table log_errors_tab;
0
7637
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7558
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8072
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7605
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7924
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6195
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5462
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
1
2055
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
884
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.