473,387 Members | 1,510 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Triggers - can I have it fire only if certain columns actually change?

I can't find it anywhere in the manual, but I have a developer that
wants to know if we can code a trigger to capture the data that has
changed in a table only if certain columns have changed. It looks like
I can do it with the when clause and check the old vs new, but that
would get very ugly with a large table. Has anyone done something like
this?

Nov 12 '05 #1
3 5685
Yes, you can specify columns in the create trigger statement like this:
create trigger <trigger name> after update of col1, col2, col3 on <table>
........

Regards
Odd B Andersen
ErgoGroup AS
Oslo, Norway

"dataguy" <ba*********@progressive.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
I can't find it anywhere in the manual, but I have a developer that
wants to know if we can code a trigger to capture the data that has
changed in a table only if certain columns have changed. It looks like
I can do it with the when clause and check the old vs new, but that
would get very ugly with a large table. Has anyone done something like
this?

Nov 12 '05 #2
dataguy wrote:
I can't find it anywhere in the manual, but I have a developer that
wants to know if we can code a trigger to capture the data that has
changed in a table only if certain columns have changed. It looks like
I can do it with the when clause and check the old vs new, but that
would get very ugly with a large table. Has anyone done something like
this?

You have to comper ateh old and new values (make sure you handle NULLs,
too).
Note that you can have more than one trigger of a kind per table.
If teh code partitions well (it often does) you simply create a trigger
per column or per group of columns also exploiting the column clause for
an UPDATE trigger.

Cheers
Serge

--
Serge Rielau
DB2 SQL Compiler Development
IBM Toronto Lab
Nov 12 '05 #3
RdR
We normally do this to pass on to MQ Series the row change based on the
column that changed so that MQ based on its rules can deliver the data to
the right message queue.

I do not know what platform you are in but you can do something to the
equivalent of a DB2 Instrumentation Facility Interface read on the logs, the
before Image will have a UB image designation in ne of the log information
and the after Image will have a UP and then in your trigger, call a stored
procedure that will compare the UB and the UP and then pass any differences
on the trigger variables any differences. Your trigger must be prepared to
handle multiple column changes because it is possible that there are more
than one changed field on a single row.

You are correct, it will get very ugly if you start comparing data pulled
from the table itself, so our approach was to get it from the DB2 logs. The
basic steps are:

1) Turn on Logging for the table you wil ldetect the column change.
2) Have an update trigger that will:
a) Call a program to do the DB2 IFI using an IFCID306 to read the
log for this particular table.
b) Read the UB and UP portion of the read result.
c) Compare every column and dump the differences in a flat file.
d) Get the after image from a flat file.
** Since we are forwarding the info to MQ, we proceeded with the following
steps bus based on what you need, you can proceed appropriate to what you
need to accomplish**
e) Format the information gathered in the correct XML format (in our
case it is XML).
f) Deposit this XML to the message queue.
g) Go back to the trigger and report that it is done, otherwise send
an error.
h) Once in the message queue, th erules we set in can forward the
xml document to the right queue or database based on the changed column
information.

If you do not want to write that program, you can use Q Replication and
doing the DB2 IFI will be handled by it, you will just have to write the
program detecting the differences on the rows of the before and after
images. The queue delivery I believe will also be handled by Q Replication.

Hope this helps.

RdR

"dataguy" <ba*********@progressive.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
I can't find it anywhere in the manual, but I have a developer that
wants to know if we can code a trigger to capture the data that has
changed in a table only if certain columns have changed. It looks like
I can do it with the when clause and check the old vs new, but that
would get very ugly with a large table. Has anyone done something like
this?

Nov 12 '05 #4

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

Similar topics

3
by: Keith | last post by:
Not sure if anyone in here knows the answer to this, but I asked in a SQL group and haven't had a suitable answer and since the front end app is ASP I though I'd give here a try. I am trying to...
11
by: raulgz | last post by:
I need audit triggers that change columns value in the same record that fire trigger. I need how to do.. Thanks..
0
by: Dave Sisk | last post by:
Hi Folks: I've got this scenario: User table/file....external triggers declared for *AFTER *INSERT, *AFTER *UPDATE, and *BEFORE *DELETE. The 3 external triggers call a SQLRPG ILE stub...
4
by: Re_endian_miss | last post by:
I know even less about databases than I thought... I always thought that the idea behind a transaction was that the actual table in question does NOT get updated until a commit is issued (either...
1
by: Christoph Graf | last post by:
Hi everybody! As far as I have seen you can inherit from a table and get its columns. Is there a possibility to also inherit a tables triggers? When I simply derive a table from another I...
1
by: Mark Flippin | last post by:
I'm evidently not understanding nested triggers and I'm looking for some help. I've an Invoice table (see below) that I want to enforce two actions via after triggers. The first trigger...
4
by: JoeyD | last post by:
With SQL 2005, did Microsoft introduce login triggers? I seem to remember hearing something about it but, I don't remember exactly what I heard. Thanks, JD
19
by: Daniela Roman | last post by:
Hello, I try to fire an event under a button click event and maybe anybody can give a clue please. I have let's say a WEB grid with PageIndexChanged event: private void...
7
by: ajomara | last post by:
Hai everyboby i am new to this group,can one help me in triggers could anyone help in the fallowing lines "Database triggers can be system triggers on a database or a schema. With a...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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,...
0
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...

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.