      $SET db2(db=db2demo qualifier=demo strdel=quote) charset(ebcdic)
      *---------------------------------------------------------------
      *--- Copyright (C) 1992-2006 Micro Focus International Ltd.
      *--- All rights reserved.
      *---------------------------------------------------------------
      *--- Static SQL example - static SELECT
      *---------------------------------------------------------------
       IDENTIFICATION DIVISION.
       PROGRAM-ID.    HCOBATCH.

       DATA DIVISION.
       WORKING-STORAGE SECTION.

       01  PRODUCT-LINE.
           05 FILLER                   PIC X(010) VALUE
              'Product = '.
           05 DISPLAY-ID               PIC X(004) VALUE ' '.
           05 FILLER                   PIC X(010) VALUE
              '   Desc = '.
           05 DISPLAY-DESC             PIC X(035) VALUE ' '.
           05 FILLER                   PIC X(007) VALUE
              ' Amt = '.
           05 DISPLAY-AMT              PIC $,$$$,$$9.99.

       01  IX                          PIC S9(04) COMP.
       01  ERR-REC-LTH                 PIC S9(09) COMP.
       01  ERR-MSG-AREA.
           05 ERR-MSG-LTH              PIC S9(04) COMP.
           05 ERR-MSG-TEXT             PIC X(700).
           05 ERR-MSG-TBL    REDEFINES ERR-MSG-TEXT.
              10 ERR-TEXT              PIC X(070) OCCURS 10 TIMES.

           EXEC SQL INCLUDE SQLCA      END-EXEC.
      *--- FILLER added to show no error when SQLCA > 136 bytes
           03 FILLER                   PIC X(010) VALUE 'END SQLCA'.
           EXEC SQL INCLUDE PRODUCT    END-EXEC.

      *---------------------------------------------------------------
      *--- Define CURSORs
      *---------------------------------------------------------------

      *--- HCO examines SQL statements and replaces all "-" in cursor
      *--- names to "_" which are valid in DB2 UDB
      *---
      *--- HCO also examines SQL statements and qualifies all table
      *--- references if you specify the DB2 QUALIFIER directive.
      *--- In this case, you would need to specify DB2(QUALIFIER=DEMO)
      *--- to compile if your logon id is anything other than DEMO.
      *--- You can eliminate this directive by creating ALIAS entries
      *--- for each table.

           EXEC SQL
                DECLARE CSR-01 CURSOR FOR
                 SELECT *
                   FROM PRODUCT
                   WHERE PROD_ID >= :PRD-PROD-ID
      ****         WHERE PROD_ID >= "1000"
                 ORDER BY PROD_ID, PROD_DESC
           END-EXEC.

      *---------------------------------------------------------------
      *--- Static SQL example - static SELECT
      *---------------------------------------------------------------

       PROCEDURE DIVISION.

       0000-MAIN-LOGIC.

           DISPLAY 'BEG HCOBATCH TEST'

           EXEC SQL
                CONNECT TO DB2DEMO
           END-EXEC

      *--- Demonstrate a static SELECT

           MOVE '1000'                 TO PRD-PROD-ID

           EXEC SQL
                OPEN CSR-01
           END-EXEC

           IF SQLCODE NOT = 0
               PERFORM 9999-SQL-ERR  THRU 9999-EXIT
           END-IF

           PERFORM 2000-FETCH-CSR    THRU 2000-EXIT
             UNTIL SQLCODE NOT = 0

           EXEC SQL
                CLOSE CSR-01
           END-EXEC

           DISPLAY 'END HCOBATCH TEST'

           GOBACK.

      *---------------------------------------------------------------
      *--- Get data from table
      *---------------------------------------------------------------

       2000-FETCH-CSR.

           MOVE SPACES                 TO PRD-PROD-DESC-TEXT

           EXEC SQL
                FETCH CSR-01
                 INTO :DCLPRODUCT
           END-EXEC

           IF SQLCODE = 0
               MOVE PRD-PROD-ID        TO DISPLAY-ID
               MOVE PRD-PROD-DESC-TEXT TO DISPLAY-DESC
               MOVE PRD-AMT-EACH       TO DISPLAY-AMT
               DISPLAY PRODUCT-LINE
           ELSE
               IF SQLCODE NOT = +100
                   PERFORM 9999-SQL-ERR THRU 9999-EXIT
               END-IF
           END-IF.

       2000-EXIT.
           EXIT.

      *---------------------------------------------------------------
      *--- Call DSNTIAR to get SQL Error Message text
      *---------------------------------------------------------------

       9999-SQL-ERR.

           MOVE 70                     TO ERR-REC-LTH
           MOVE 700                    TO ERR-MSG-LTH

           CALL 'DSNTIAR'           USING SQLCA
                                          ERR-MSG-AREA
                                          ERR-REC-LTH

           PERFORM VARYING IX FROM 1 BY 1 UNTIL IX > 10
               DISPLAY ERR-TEXT (IX)
           END-PERFORM.

       9999-EXIT.
           EXIT.
