This example employs a trigger program to automatically generate a unique employee number every time a new record is added to an Employee Master File called EMPLOYEE. The file contains fields you would expect to see in a simple employee file, such as employee number, name, address, job code, salary, etc. Figure 6.1 shows the complete layout for the file.
The trigger program is added to the file via a CL command in the program DB006C, as shown in Listing 6.1.
PGM /*---------------------------------------------------------------*/ /* Add trigger DB006R to file Employee to auto-generate */ /* the employee number key. */ /*---------------------------------------------------------------*/ ADDPFTRG FILE(EMPLOYEE) TRGTIME(*BEFORE)+ TRGEVENT(*INSERT) PGM(DB006R) ALWREPCHG(*YES) ENDPGM
The trigger program DB006R is shown in Listing 6.2.
*----------------------------------------------------------------- * Data Area EmployeeDA contains the last employee number * used. *----------------------------------------------------------------- DEmployeeDA DS Dtaara(EmployeeDA) D Last# 9S 0 * DpBefore S * DpAfter S * * DBefore E DS ExtName(EMPLOYEE) Prefix(B_) D Based(pBefore) DAfter E DS ExtName(EMPLOYEE) Prefix(A_) D Based(pAfter) D* *----------------------------------------------------------------- * Trigger Buffer and Trigger Buffer Length Declarations *----------------------------------------------------------------- DBufferLen S 10I 0 DTrigBuff DS D TrigFile 10A D TrigLib 10A D TrigMbr 10A D TrigEvent 1A D TrigTime 1A D TrigCommit 1A D TrigRes1 3A D TrigCCSID 10I 0 D TrigRRN 10I 0 D TrigRes2 4A D TrigB4OS 10I 0 D TrigB4Len 10I 0 D TrigB4NBM 10I 0 D TrigB4NBL 10I 0 D TrigAftOS 10I 0 D TrigAftLen 10I 0 D TrigAfNBM 10I 0 D TrigAfNBL 10I 0 *----------------------------------------------------------------- * Trigger Constants *----------------------------------------------------------------- D@Insert C '1' D@Delete C '2' D@Update C '3' D@Before C '2' D@After C '1' *----------------------------------------------------------------- * Input parameters are passed automatically when the trigger * fires. Passed are the trigger buffer and trigger buffer length. *----------------------------------------------------------------- C *Entry PList C Parm TrigBuff C Parm BufferLen * *----------------------------------------------------------------- * Map the data structures for the before and after images to * the offset location in the trigger buffer using pointers. *----------------------------------------------------------------- C Eval pBefore = %Addr(TrigBuff) + TrigB40S C Eval pAfter = %Addr(TrigBuff) + TrigAft0S * *----------------------------------------------------------------- * Only assign employee number on inserts. *----------------------------------------------------------------- C If TrigEvent = @Insert *----------------------------------------------------------------- * Get the last employee number from the data area. Increment * it and update data area and trigger buffer with new number. *----------------------------------------------------------------- C *Lock In EmployeeDA C Eval Last# = Last# + 1 C Out EmployeeDA C Eval A_EMEMP# = Last# C Endif * C Return