Wednesday, August 12, 2015

Great example of CREATE TRIGGER with declear set if statment


************************INSERT*****************************************

Create  trigger IBUNDELDERIVED
               AFTER INSERT ON CATENTREL REFERENCING NEW AS N
               FOR EACH ROW MODE DB2SQL
               BEGIN ATOMIC
                           declare reltype CHAR(32);
                           declare existing INT;
                           set reltype=TRIM(N.CATRELTYPE_ID);
                           if  (reltype is not NULL AND reltype  = 'BUNDLE_COMPONENT')
    THEN
                SET existing=(select count(*) from X_BUNDLE_DERIVED where BUNDLE_CATENTRY_ID=N.CATENTRY_ID_PARENT and ATTRIBUTE='Sequence');
                if (existing=0)
                then
                        insert into X_BUNDLE_DERIVED(BUNDLE_CATENTRY_ID,ATTRIBUTE,DERIVATION_STATUS)
                                VALUES (N.CATENTRY_ID_PARENT,'Sequence','UnProcessed');
                end if;
        end if;
               END#

********************************UPDATE*************************************
CREATE TRIGGER UBUNDELDERIVED
AFTER UPDATE ON CATENTREL REFERENCING NEW AS N OLD AS O
FOR EACH ROW MODE DB2SQL
BEGIN ATOMIC

declare reltype CHAR(32);
declare existing INT;
set reltype=TRIM(N.CATRELTYPE_ID);

if  (reltype is not null and reltype  = 'BUNDLE_COMPONENT')
    then
SET existing=(select count(*) from X_BUNDLE_DERIVED where BUNDLE_CATENTRY_ID=N.CATENTRY_ID_PARENT and ATTRIBUTE='Sequence');
if (existing =0)
then
insert into X_BUNDLE_DERIVED(BUNDLE_CATENTRY_ID,ATTRIBUTE,DERIVATION_STATUS)
values(N.CATENTRY_ID_PARENT,'Sequence','UnProcessed');
end if;
end if;
 END#

No comments:

Post a Comment