Stolce,
thanks for idea.
I have written first trigger "after delete" on mytable1:
Code:
CREATE TRIGGER DB2ADMIN.MYTABLE1_D AFTER DELETE ON DB2ADMIN.MYTABLE1 FOR EACH STATEMENT MODE DB2SQL
SELECT CASE WHEN COUNT(*)>0 THEN RAISE_ERROR('80001','Duplicate values after delete') ELSE 0 END FROM SYSIBM.SYSDUMMY1 WHERE EXISTS
(
SELECT
COL,
MYCOUNT
FROM (
SELECT
COL,
COUNT(*) AS MYCOUNT
FROM
DB2ADMIN.MYTABLESUM
GROUP BY COL
) AS TEMP
WHERE
(
COL,
MYCOUNT
)
NOT IN
(
SELECT
COL,
SUM(MYCOUNT)
FROM
(
SELECT COL, COUNT(*) AS MYCOUNT FROM DB2ADMIN.MYTABLE1 GROUP BY COL
UNION ALL
SELECT COL, COUNT(*) AS MYCOUNT FROM DB2ADMIN.MYTABLE2 GROUP BY COL
) AS TEMP2
GROUP BY COL
)
)
The same trigger logic for "after insert, update" for all mytable tables.
But there is a one problem: trigger is executed (triggered)
after each delete and therefore it always return error after delete statement. I would need to trigger it at the end of transaction not after each delete.
Simple sample:
Table mytable1 has record: 1
Table mytabe2 has record: 2
Table mytabesum has: 1, 2
If I would like to delete record 1 from mytable1 (delete from db2admin.mytable where col =1) then trigger fires up and returns error, this is obvious because I also need to delete the same data from mytablesum (delete from db2admin.mytablesum where col=1). So trigger is fired up after each delete statement, but I would need some kind of functionality when trigger should be executed after transaction.
Sample1 (this functionality I would need):
First step: delete from mytable1 where col1=1
Second step: delete from mytablesum where col1=1
Third step: check consistency.
But triggers work like this. Sample2:
First step: delete from mytable1 where col1=1
Second step: check consistency.
Third step: delete from mytablesum where col1=1
Forth step: check consistency.
So second step returns error. Is there any functionality to have sample1 functionality?
I don't think such a trigger exists. So I don't think trigger is a right way of solving consistency (integrity) of data in my case.
Any idea how to solve the problem?
Thanks,
Grofaty