473,763 Members | 6,401 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 4198
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
2004
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
6556
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
3461
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
7144
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 tables because other programs perform updates to these tables and we do not want the triggers to fire...
0
7147
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 manually call the stored proc, and the external trigger is created without any errors. However, when I do...
6
9252
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
2477
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 should be fairly generic. - I use a C stored procedure GETJOBNAME to get some extra audit data,
1
2212
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
9312
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 this - by iterating through the collection of tables and passing the tablename to something that...
11
7878
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
10144
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9997
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9937
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
9822
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8821
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6642
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5270
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3917
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
3
3522
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.