Post by c***@talgentra.comHi Knut Stolze/tonkuma,
What I'm trying to achieve here is to build a generic trigger that can be
plugged in to A table with any column name. The table is used to stored
all kind of flags with ACCOUNTID as Primary Key. The requirement here is
if Any flag in this table is updated with 'Y', then it will perform
neccessary action.
The table structure will most likely look like this:-
ACCOUNTID DECIMAL(10,0),
FLAG1 VARCHAR(1),
FLAG2 VARCHAR(1),
FLAG3 VARCHAR(1),
FLAG4 VARCHAR(1)
The user will add new flags in future, so my trigger need to take care of
future changes (with unknown column name yet) as well. I think of using a
cursor to retrieve all the column name as follow:-
CREATE TRIGGER TALLYMAN.VC
AFTER UPDATE ON TALLYMAN.VC_BULKUPDATES
REFERENCING OLD AS O NEW AS N
OLD_TABLE AS OTAB NEW_TABLE AS NTAB
FOR EACH ROW
BEGIN ATOMIC
DECLARE v_Query VARCHAR(3000);
DECLARE v_columnName, v_ChangeColumn VARCHAR(100);
DECLARE v_NewFlag, v_OldFlag VARCHAR(200);
DECLARE V_CNT INTEGER default 0;
FOR c_Col AS
SELECT NAME from SYSIBM.SYSCOLUMNS
WHERE TBNAME='VC_BULKUPDATES'
AND UPPER(NAME) <> 'ACCOUNTID'
DO
SET v_ColumnName = c_Col.NAME;
SET v_NewFlag = ? ** need to assign New value of FLAG1, FLAG2 ..
SET v_OldFlag = ? ** need to assign Old value of FLAG1, FLAG2 ..
IF (v_NewFlag = 'Y' AND v_OldFlag <> 'Y') THEN
CALL ...
...
ELSE
...
...
END IF;
END FOR;
END
And i'm successfully compile the trigger but is the most challenging time
for me to implement the logic.
I'm facing problem to assign the value to v_NewFlag and v_OldFlag for each
of the column. Any idea how can i assign it? Or maybe my program flow are
not correct?
trigger. DB2 cannot know what the declared data type of a column to be
added in the future will be. Therefore, it cannot deal with such
non-existing columns when the trigger is created initially.