Skip to main content

 
IBM Power Systems software  >  IBM i  > Software  > 

IBM DB2 for i

  

DB2 Example: Referential Integrity with RESTRICT Rule

     *========================================================*
      * This program is intended to illustrate a program       *
      * that has changed to use a referential integrity        *
      * RESTRICT rule rather than checking itself.             *
      * Verify that adding an employee to the EMPLOYEE file    *
      * is assigned to a valid department from the DEPARTMENT  *
      * file .                                                 *
      *                                                        *
      * This program will only work for files that do not      *
      * contain null values. If a field contains null values,  *
      * SQL must be used to write to the file.                 *
      *                                                        *
      *                                                        *
      * CRTBNDRPG PGM(CORPDATA/RI02) SRCFILE(MJASRC/RPG)       *
      *           DBGVIEW(*SOURCE) ALWNULL(*YES)               *
      *                                                        *
      * ADDPFCST FILE(CORPDATA/DEPARTMENT)                     *
      *          TYPE(*PRIKEY) KEY(DEPTNO)                     *
      *                                                        *
      * ADDPFCST FILE(CORPDATA/EMPLOYEE)                       *
      *          TYPE(*REFCST) KEY(WORKDEPT)                   *
      *          CST(VERIFY_WORKDEPT)                          *
      *          PRNFILE(CORPDATA/DEPARTMENT) PRNKEY(DEPTNO)   *
      *          DLTRULE(*RESTRICT) UPDRULE(*RESTRICT)         *
      *                                                        *
      *                                                        *
      *========================================================*
      *
      *========================================================*
      * File definition used for insert.                       *
      *========================================================*
     FEMPDSP    CF   E             WORKSTN
     FEMPLOYEE  O    E           K DISK    RENAME(EMPLOYEE:EMPFMT)
     F                                     INFDS(FDBF)
      *========================================================*
      * The file information data structure will be used       *
      * to deterimine if the referential restrict rule is      *
      * violated.                                              *
      *========================================================*
     DFDBF             DS
     D  STATUS           *STATUS
     D  MSGID                 46     52A
      *========================================================*
      * Get a new employee record from the display.            *
      * Loop until the department is valid.                    *
      *========================================================*
     C                   MOVE      *ON           *IN61
     C                   DOW       *IN61
     C                   EXFMT     A
      *========================================================*
      * Write to the EMPLOYEE file. Referential integrity will *
      * check to see if a department for the employee exists   *
      * If it does not, return a message to the screen.        *
      * In V3R6, this same technique can be used to detect a   *
      * trigger error via 01023 (*BEFORE) or 01024 (*AFTER)    *
      *========================================================*
     C                   WRITE     EMPFMT                               61
     C     STATUS        IFEQ      1022
     C                   MOVE      *ON           *IN60
     C                   ELSE
     C                   MOVE      *OFF          *IN60
     C                   ENDIF
     C                   ENDDO
      *
     C                   RETURN