473,796 Members | 2,536 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

DB2 v9.5 Trigger Question

1 New Member
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_EFFDAT E_ADJUST_DATE TIMESTAMP
DEFAULT CURRENT TIMESTAMP;

CREATE TRIGGER "BLOCK_BI_EFFDA TE_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_CUTOF F" )
BEGIN ATOMIC
SET gv_BLOCK_EFFDAT E_ADJ_DATE = N.EFFDATE;
SET (N.EFF_DATE) = (SELECT MIN(E.EFF_DATE)
FROM "DATE_CUTOF F" );
END;

CREATE TRIGGER "BLOCK_AI_EFFDA TE_ADJ"
AFTER INSERT
ON ADJ
REFERENCING
NEW AS "N"
FOR EACH ROW
WHEN (gv_BLOCK_EFFDA TE_ADJ_DATE < (SELECT MIN
(E.EFF_DATE) FROM "DATE_CUTOF F" ))
BEGIN ATOMIC
INSERT INTO NOTES
(RECORD_DATE,US ER_ID,ROWID,COM MENT,TRANMEMO)
VALUES( CURRENT TIMESTAMP,USER_ ID,NEXTVAL FOR
SEQ_NOTES),'SYS TEM 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 1878

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

Similar topics

2
9193
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
2136
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 (with different names) that need a trigger that does this and pl'pgsql can't expand variable names to fieldnames. Writing a dozen functions (one per columnname) is /way/ too blunt so I tried pl/tcl (which I don't know): ...
1
4463
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 function created to do this task. The function I am trying to create is as follows: CREATE FUNCTION customer_bak_proc(integer) RETURNS boolean as 'INSERT INTO customer_bak (SELECT * from customer where id = $1 )' LANGUAGE 'SQL';
4
4184
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 trap the errors properly? CREATE TRIGGER myschema.t1_upd_t AFTER UPDATE OF dt ON myschema.t1 REFERENCING NEW AS N
3
4953
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 fails the trigger's test, the trigger rolls back the INSERT command and no changes are made to the database. As far as my object is concerned, the transaction went through either way (no matter what the trigger did). What I need is for the object...
1
1569
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 case? (I would need this) e.g. USER INSERT
1
1222
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 the number in another field on the same table, see my example that follows: Before Trigger Table: Job J_User1: 0122567801 J_JobType: 20
9
2009
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 will be appreciated.
10
3574
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 STATEMENT trigger that would insert all the deleted rows in one operation like this: CREATE TRIGGER MyTable_TD
2
1397
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 to get around. We use two fields for this trigger. Both fields are 3 digit numbers (varchar(3) in the database, I have no control over that). The second field is used to store the value of the first field only if the first field has changed, and...
0
9680
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10456
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...
1
10174
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
10012
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...
1
7548
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6788
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
5442
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
4118
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
2926
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.