Using a Trigger to Auto-Generate a Key Value

发表于:2007-05-26来源:作者:点击数: 标签:
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. Example: Using a Trigger to Auto-Generate a Key Value This example emplo
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.

Example: Using a Trigger to Auto-Generate a Key Value

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.

Click To expand
Figure 6.1: This Employee Master File is used to demonstrate trigger usage.

The trigger program is added to the file via a CL command in the program DB006C, as shown in Listing 6.1.

Listing 6.1: CL program DB006C uses ADDPFTRG to add a trigger.
Start example
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 
End example

The trigger program DB006R is shown in Listing 6.2.

Listing 6.2: ILE RPG program DB006R is a trigger for file EMPLOYEE.
Start example
      *-----------------------------------------------------------------
      *   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

原文转自:http://www.ltesting.net