473,387 Members | 1,621 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.

DB2 v9.5 Trigger Question

Hello,

I have a request that I thought was straightforward and would be fairly simple. After many hours of research and trying various triggers, procedures and variables, I have not been able to find a solution.

I need to evaluate a timestamp column to be in an acceptable range on insert/update of a table. If the value is out of the acceptable range, I need to update it to an acceptable value and then write a record into an audit table recording the change.

I began with a BEFORE INSERT trigger, added a WHEN condition and SET the value, no problem, but I cannot insert a new record into another table in a BEFORE trigger. So, I wrote 2 triggers, SET the timestamp in the BEFORE trigger, and INSERT the audit record in the AFTER trigger. This does not work because the WHEN condition in the AFTER trigger fails because the BEFORE trigger has modified the value so I do not know when I need to write the audit record.

I then tried moving the INSERT into a DB2 procedure that I called from the BEFORE trigger, but I learned you cannot call a procedure with 'MODIFIES SQL DATA' from a BEFORE trigger.

Back to the drawing board,my latest attempt is creating a GLOBAL VARIABLE that is SET in the BEFORE trigger that I can then evaluate in the AFTER trigger WHEN condition to know when to write the audit record. I have not been able to SET the GLOBAL VARIABLE in the BEFORE trigger, I get a -20430 SQLCODE "A global variable cannot be set or applied in this context."

I am running out of ideas on how to accomplish this. Conceptually it seems to be simple.

Here is the latest trigger / global variable statement I have:

CREATE VARIABLE gv_BLOCK_EFFDATE_ADJUST_DATE TIMESTAMP
DEFAULT CURRENT TIMESTAMP;

CREATE TRIGGER "BLOCK_BI_EFFDATE_ADJ"
NO CASCADE BEFORE INSERT
ON ADJ
REFERENCING
NEW AS "N"
FOR EACH ROW
WHEN (N.EFF_DATE < (SELECT MIN(E.EFF_DATE) FROM "DATE_CUTOFF" )
BEGIN ATOMIC
SET gv_BLOCK_EFFDATE_ADJ_DATE = N.EFFDATE;
SET (N.EFF_DATE) = (SELECT MIN(E.EFF_DATE)
FROM "DATE_CUTOFF" );
END;

CREATE TRIGGER "BLOCK_AI_EFFDATE_ADJ"
AFTER INSERT
ON ADJ
REFERENCING
NEW AS "N"
FOR EACH ROW
WHEN (gv_BLOCK_EFFDATE_ADJ_DATE < (SELECT MIN
(E.EFF_DATE) FROM "DATE_CUTOFF" ))
BEGIN ATOMIC
INSERT INTO NOTES
(RECORD_DATE,USER_ID,ROWID,COMMENT,TRANMEMO)
VALUES( CURRENT TIMESTAMP,USER_ID,NEXTVAL FOR
SEQ_NOTES),'SYSTEM MODIFIED EFFDATE', ' THE
SYSTEM MODIFIED THE EFFECTIVE DATE TO BE EQUAL
TO THE INCEPTION DATE.');
END;

COMMIT;


Any advice or ideas will be greatly appreciated!!
Jan 11 '11 #1
0 1863

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: imani_technology | last post by:
How would I write a trigger that updates the values of a Description column to upper case for even IDs and to lower case for odd IDs? I need this trigger to fire for INSERT and UPDATE events.
4
by: Jules Alberts | last post by:
Hello everyone, I'm working on a tiny trigger function that needs to ensure that all values entered in a field are lowercase'd. I can't use pl/pgsql because I have a dozen different columns...
1
by: Barbara Lindsey | last post by:
I am a postgres newbie. I am trying to create a trigger that will put a copy of a record into a backup table before update or delete. As I understand it, in order to do this I must have a...
4
by: Alexander Pope | last post by:
I am using db2 udb v8.2 AIX I have created trigger, however I am not confident it meets industry standards. If I make it fail, I cant tell from the message where it is failing. what can I add to...
3
by: ChrisN | last post by:
Hello all, I have a quick question. I'm using a C# object to commit new rows to a database. In the database I have an INSERT Trigger watching values come in. If the record to be committed...
1
by: Daniel Schuchardt | last post by:
Hi list, i'm a bit confused. I have a table with a trigger after insert. It is possible that this trigger will do inserts in the same table, but is the after insert trigger fired again in this...
1
by: majastic | last post by:
Hello all, I have a database question for you. I have a table in SQL 2000 that I think I need a trigger for. I'm tring to get the trigger to take the last two numbers from one field and replace...
9
by: Chico Che | last post by:
Have a table that has following fields (pkid, field1, field2, field3, field4). I need to create a trigger that will insert a row into another table with the pkid column that was updated. Any help...
10
by: JohnO | last post by:
Hi All, This question is related to iSeries V5R4 and db2. I want to implement an AFTER DELETE trigger to save the deleted rows to an archive table, I initially defined it as a FOR EACH...
2
by: somacore | last post by:
Hello all, I am trying to implement a trigger on a Microsoft SQL Server 2000 database. I have created the trigger, and it works mostly like it should, however I have a slight problem I can't seem...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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,...
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.