473,326 Members | 2,168 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,326 software developers and data experts.

Trigger help

Need help to write a trigger according to the following business
requirement. This on DB2 UDB V8.2 / AIX 5.3

Whenever a 100th record is inserted into my 'ACCOUNT' table with a
particular 'BATCH_ID' I need to either update or insert a row onto
'DESCRIPTION column of STATUS' table as verified for that particular
BATCH_ID.
Any help is greatly appreciated.
Thanks,
Sam

Mar 26 '07 #1
3 1897
Sam Durai wrote:
Need help to write a trigger according to the following business
requirement. This on DB2 UDB V8.2 / AIX 5.3

Whenever a 100th record is inserted into my 'ACCOUNT' table with a
particular 'BATCH_ID' I need to either update or insert a row onto
'DESCRIPTION column of STATUS' table as verified for that particular
BATCH_ID.
Have you actually tried something? If so, what? And why did it fail?

CREATE TRIGGER t
AFTER INSERT ON ...
FOR EACH STATEMENT
WHEN ( MOD( ( SELECT COUNT(*) FROM ... WHERE batch_id = ... ), 100) )
MERGE ...

--
Knut Stolze
DB2 z/OS Utilities Development
IBM Germany
Mar 26 '07 #2
Sam Durai wrote:
Need help to write a trigger according to the following business
requirement. This on DB2 UDB V8.2 / AIX 5.3

Whenever a 100th record is inserted into my 'ACCOUNT' table with a
particular 'BATCH_ID' I need to either update or insert a row onto
'DESCRIPTION column of STATUS' table as verified for that particular
BATCH_ID.
First, as Knuth indicated in his post, you often get better answers if
you provide what you have done so far. Second, why do you want to record
each 100 update? Would it not be better to record each update and divide
that by 100 when you inspect the table?

Anyhow, can you guarantee that your account's are inserted one by one,
as in:

insert into account (...) values (...) ?

Otherwise I think it will be rather difficult to do what you describe
with one trigger. The best I can come up with involves an extra table
and an extra trigger. The code looks very error prone and I would think
twice before using it in any critical situation:

drop table account @
create table account (
account_id int not null
GENERATED BY DEFAULT AS IDENTITY
(START WITH 1, INCREMENT BY 1) primary key,
batch_id int not null
)@

drop table status @
create table status (
batch_id int not null primary key,
description varchar(10) not null
)@

drop table detector @
create table detector (
batch_id int not null primary key,
cnt int not null
)@

drop trigger a @
create trigger a
after insert on account
referencing new as n
for each row
begin atomic
merge into detector d
using (
select batch_id, count(1) from account
where batch_id = n.batch_id
group by batch_id
) X (batch_id, cnt)
on X.batch_id = d.batch_id
when matched then
update set cnt = X.cnt
when not matched then
insert values (x.batch_id, X.cnt);
end @

drop trigger b @
create trigger b
after update on detector
referencing old as o new as n
for each row
when ((n.cnt / 100) (o.cnt / 100))
begin atomic
merge into status s
using lateral(values(n.batch_id, char(n.cnt / 100)))
X (batch_id, description)
on X.batch_id = S.batch_id
when matched then
update set description = X.description
when not matched then
insert values (x.batch_id, X.description);
end @
/Lennart

Mar 26 '07 #3
>Whenever a 100th record [sic] is inserted into my 'ACCOUNT' table with a particular 'BATCH_ID' I need to either update or insert a row onto 'DESCRIPTION column of STATUS' table as verified for that particular BATCH_ID. <<

1) Please post DDL, so that people do not have to guess what the keys,
constraints, Declarative Referential Integrity, data types, etc. in
your schema are. Sample data is also a good idea, along with clear
specifications. It is very hard to debug code when you do not let us
see it.

2) Because you do not understand that rows are NOT records, you are
looking for file system solution that would materalize the fact via
procedrual code. Think in terms of tables instead and use a VIEW that
is always correct.

CREATE VIEW Account_100s (..)
AS
(SELECT *, rn
FROM (SELECT .. -- list columns in production code
ROW NUMBER()
OVER(PARTITION BY acct_nbr ORDER BY posting_date) AS rn
FROM Accounts) AS A (.., rn)
WHERE MOD(rn, 100) = 0;

3) Read ISO-11179 rules for data element names. Status is a kind of
attribute (martial _staus, shipping_status, , etc.) and not an
entity. Likewise a description has to describe something in
particular, etc.

Mar 27 '07 #4

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

Similar topics

9
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,...
3
by: takilroy | last post by:
Hi, Does anyone know of a simple way to do this? I want to create an insert trigger for a table and if the record already exists based on some criteria, I want to update the table with the...
33
by: coosa | last post by:
I have a table: ---------------------------------------------------- CREATE TABLE CATEGORY ( CATEGORY_ID INTEGER IDENTITY(1,1) NOT NULL, CATEGORY_NAME VARCHAR(40) NOT NULL,...
4
by: SUKRU | last post by:
Hello everybody. Unfortunately I am pretty new to sql-server 2000 I need some help with a Trigger I created. I created a trigger witch takes the id of the affected row and does a update on a...
1
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...
2
by: mob1012 via DBMonster.com | last post by:
Hi All, I wrote last week about a trigger problem I was having. I want a trigger to produce a unique id to be used as a primary key for my table. I used the advice I received, but the trigger is...
7
by: Shane | last post by:
I have been instructed to write a trigger that effectively acts as a foreign key. The point (I think) is to get me used to writing triggers that dont use the primary key(s) I have created the...
11
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'...
0
Brad Orders
by: Brad Orders | last post by:
Hi all Here is my situation: When table A is updated, I need to record some data in table B, then apply the update to table A Normally I would use a FOR UPDATE trigger, but the table has a...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.