Easily maintain SQL triggers

This tip provides info to help you maintain SQL triggers -- without the headaches.

This Content Component encountered an error

There is almost always a problem when creating an SQL trigger and the trigger's file is in use. I found out that using a UDF to do the required computation makes this process much easier. Exclusive lock of trigger file is needed only once. Later you can test and change your UDF without any need to recreate the trigger program -- in the foll. Example lot no in FSO file is determined after an allocation is created in ECL file. This logic...

is entered into the trigger program, which calls the nextlot() UDF for newlot value.


 
 ** Beginning of Data ******************************
 CREATE TRIGGER LOV64MMO/ELAWOTRGR                                          
AFTER INSERT ON V604MMF/ELA                                                 
 REFERENCING  NEW AS N                                                      
 FOR EACH ROW MODE DB2ROW                                                   
 WHEN (SUBSTR(N.ALOT,10,1) BETWEEN 'A' AND 'Z'                              
      AND N.ATYPE = 'S' AND N.LQALL > 0 )                                   
 BEGIN                                                                      
 DECLARE REF CHAR(5);                                                       
 SET REF = (SELECT IREF05 FROM IIM WHERE IPROD = N.APROD);                  
 IF REF ='MAIN' THEN                                                        
 UPDATE V604MMF/FSO                                                         
     SET SOLOT = NEXTLOT(N.ALOT),                                           
         SOPLOT = '1'                                                       
 WHERE SORD = N.AORD AND SOFAC = 'R1';                                      
 END IF;                                                                    
 END                                                                        
 ** End of Data  ******************************
                                         CREATE FUNCTION  QGPL/NEXTLOT                                                                 
 (ILOT  CHAR(10))                                                                              
 RETURNS CHAR (10)                                                                             
 LANGUAGE SQL                                                                                  
 BEGIN                                                                                    
 DECLARE ICHR CHAR(1);                                                                        
 DECLARE MCHR CHAR(1);                                                                         
 DECLARE NCHR CHAR(1);                                                                         
 DECLARE MLOT CHAR(10);                                                                        
 DECLARE NLOT CHAR(10);                                                                        
 SET ICHR = SUBSTR(ILOT,10,1);                                                                 
 IF ICHR BETWEEN 'A' AND 'Z' THEN                                                              
 SET MLOT = (SELECT MAX(LLOT) FROM ILNL01                                                      
            WHERE SUBSTR(LLOT,1,9) = SUBSTR(ILOT,1,9));                                        
 SET MCHR = SUBSTR(MLOT,10,1);                                                                 
 SET NCHR = TRANSLATE(MCHR,'BCDEFGHIJKLMNOPQRSTUVW',                                           
                           'ABCDEFGHIJKLMNOPQRSTUV');                                          
                                                                                               
 SET NCHR = TRANSLATE(MCHR,'BCDEFGHIJKLMNOPQRSTUVW',                  
                           'ABCDEFGHIJKLMNOPQRSTUV');                 
 SET NLOT =  SUBSTR(ILOT,1,9) CONCAT NCHR;                            
 END IF;                                                              
 RETURN NLOT;                                                         
 END                                                                  
 ***************************** End of Data *************************** 

==================================
MORE INFORMATION ON THIS TOPIC
==================================

The Best Web Links: tips, tutorials and more.

Ask your programming questions--or help out your peers by answering them--in our live discussion forums.

Ask the Experts yourself: Our application development gurus are waiting to answer your programming questions.

This was first published in September 2002

Dig deeper on iSeries ILE programming

0 comments

Oldest 

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

-ADS BY GOOGLE

SearchEnterpriseLinux

SearchDataCenter

Close