Tip

Easily maintain SQL triggers

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

There are Comments. Add yours.

 
TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
Sort by: OldestNewest

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:

Disclaimer: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.