Previous Topic: Sample Procedure DefinitionNext Topic: Sample of Procedure Invocation


Sample Procedure Program

The following example shows a sample procedure program written in COBOL. This program requires the SQL employee demo database.


      *COBOL PGM SOURCE FOR GETBONUS
      *RETRIEVAL
      *DMLIST
       IDENTIFICATION DIVISION.
       PROGRAM-ID.             GETBONUS.
       AUTHOR                  DEFJE01.
       INSTALLATION.           SYSTEM71.
       DATE-WRITTEN            06/25/99
      *--------------------------------------------------------------*
      *                                                              *
      * GETBONUS will return the sum of all bonus amounts for a      *
      * given employee.                                              *
      *  Parameters:                                                 *
      * EMP_ID:    : input parameter must contain employee id        *
      * BONUS      : output parameter returns sum of bonus           *
      * CURRENCY-BONUS : output parameter returns currency symbol    *
      *          or 'ERR' in case of an error condition              *
      * These parameters are assumed to have been defined            *
      * 'WITH DEFAULT' in the procedure definition, so that null     *
      * indicators do not need to be defined and processed           *
      *--------------------------------------------------------------*
       ENVIRONMENT DIVISION.
      *
       CONFIGURATION SECTION.
      *SOURCE-COMPUTER.                IBM WITH DEBUGGING MODE.
      *
       DATA DIVISION.
      *
       WORKING-STORAGE SECTION.
       01 ERROR-STATUS                             PIC X(4).
      *--------------------------------------------------------------*
      *                                                              *
      *--------------------------------------------------------------*
       LINKAGE SECTION.
      * Procedure parameters
        EXEC SQL BEGIN DECLARE SECTION END-EXEC.
        77 EMP-ID                      PIC 9(4).
        77 BONUS                       PIC 9(10).
        EXEC SQL END DECLARE SECTION END-EXEC.
        77 CURRENCY-BONUS              PIC X(3).
      * Other parameters do not need to be specified
      *--------------------------------------------------------------*
        PROCEDURE DIVISION USING EMP-ID, BONUS, CURRENCY-BONUS.
        0000-MAINLINE.

           MOVE '$' TO CURRENCY-BONUS.

           EXEC SQL
            SELECT SUM(BONUS_AMOUNT) INTO :BONUS
             FROM DEMOEMPL.BENEFITS
             WHERE EMP_ID = :EMP-ID
           END-EXEC

           IF SQLSTATE NOT = '00000'
             MOVE 'ERR' TO CURRENCY-BONUS.

           EXIT PROGRAM.
           STOP RUN.