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

Trigger: After Delete ... for each row behaviour

Hello,

I am attempting to create a trigger to keep track of changes to a
table, mainly about specific changes to the data in the table. I had
hoped that triggers defined as for each row would actually run as each
row was deleted, but it appears that it is fired, once for each row,
after the entire delete statement has completed. Is this expected?

Here is some DDL/DML that will demonstrate what I'm trying to do - and
what the triggers are doing. If you can spot errors I'm doing or
suggestions for how to get the results I want, I'd appreciate it. Or
just confirming that this is as DB2 is supposed to work.

create table test_delete_trigger (col1 integer, col2 integer);

insert into test_delete_trigger (col1, col2)
values (1, 1), (1, 2), (1, 3), (2, 4), (2, 5);

create table tdt_history (col1 integer, col2 integer, action varchar
(8), time timestamp);

CREATE TRIGGER test_del_trig AFTER DELETE ON test_delete_trigger
REFERENCING OLD AS O FOR EACH ROW MODE DB2SQL
BEGIN ATOMIC
INSERT INTO tdt_history
(col1, col2, time, action)
VALUES (1, 2, current timestamp,
CASE WHEN 0 = (select count(*) From (select 1
from test_Delete_trigger
where col1 = o.col1 fetch first 1 row only)x )
then 'FULL_DEL'
WHEN 0 = (select count(*) From (select 1
from test_Delete_trigger
where col1 = o.col1
and col2 = o.col2 fetch first 1 row only)x )
THEN 'PART_DEL'
ELSE 'HMM-????'
END);
END
;
--This trigger is supposed to mark deletes from the table where there
are still rows in the database with the same value for COL1 as
PART_DEL, but where there are no longer any rows in the table with the
same value for COL1 as PART_DEL.

delete from test_delete_trigger where (col1, col2) in (values (2, 4));
-- This statement insert a row into TDT_HISTORY stating that a
PART_DEL happened.

delete from test_delete_trigger where (col1, col2) in (values (2, 5));
-- This statement insert a row into TDT_HISTORY stating that a
FULL_DEL happened.

delete from test_delete_trigger where col1 = 1;
-- This statement inserts 3 rows into TDT_HISTORY stating that a
FULL_DEL happened for the same COL1 at the same time.
-- I'd prefer this last one to have 2 PART_DEL, and 1 FULL_DEL,
although I wouldn't really care which was which.

Thanks,
Chris
Nov 18 '08 #1
1 8245
ChrisC wrote:
Hello,

I am attempting to create a trigger to keep track of changes to a
table, mainly about specific changes to the data in the table. I had
hoped that triggers defined as for each row would actually run as each
row was deleted, but it appears that it is fired, once for each row,
after the entire delete statement has completed. Is this expected?
Yes, that is how it is defined in ANSI.
Before the DELETE statement has been completed the database is in an
inconsistent state.

Cheers
Serge
--
Serge Rielau
DB2 Solutions Development
IBM Toronto Lab
Nov 19 '08 #2

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

Similar topics

8
by: Jason | last post by:
I have a table that matches up Securities and Exchanges. Individual securities can belong on multiple exchanges. One of the columns, named PrimaryExchangeFlag, indicates if a particular exchange is...
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...
0
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...
1
by: Robert Fitzpatrick | last post by:
I am running PostgreSQL 7.4.5 and have a trigger on a table called tblriskassessors which inserts, updates or delete a corresponding record in tblinspectors by lookup of a contact id and license...
1
by: abhi81 | last post by:
Hello All, I have a table on which I have created a insert,Update and a Delete trigger. All these triggers write a entry to another audit table with the unique key for each table and the timestamp....
2
by: dean.cochrane | last post by:
I have inherited a large application. I have a table which contains a hierarchy, like this CREATE TABLE sample_table( sample_id int NOT NULL parent_sample_id int NOT NULL ....lots of other...
6
by: Oliver | last post by:
I'm fairly new to DB2. I have been assigned to build a delete trigger that finds the data type of each of the table's fields so that the trigger can then build a string consisting of OLD values...
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...
3
by: Andy Baker | last post by:
I have set up a trigger that updates a table in another part of may database whenever a row is deleted - of the form CREATE TRIGGER t_Delete ON tblOrderDetails AFTER DELETE AS SELECT @Quantity =...
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
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: 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
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
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...
0
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...
0
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...
0
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...

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.