 IDENTIFICATION DIVISION.
 PROGRAM-ID. CURSORS-EXAMPLE.
 ENVIRONMENT DIVISION.
 DATA DIVISION.
 WORKING-STORAGE SECTION.
     EXEC SQL BEGIN DECLARE SECTION END-EXEC.
         01  USERNAME          PIC X(10) VARYING.
         01  PASSWD            PIC X(10) VARYING.
         01  EMP-REC-VARS.
             05  PRODUCT-NAME   PIC X(20) VARYING.
             05  UNIT-PRICE     PIC S9(6)V99
                            DISPLAY SIGN LEADING SEPARATE.
         EXEC SQL VAR UNIT-PRICE IS DISPLAY(8,2) END-EXEC.
      EXEC SQL END DECLARE SECTION END-EXEC.

     EXEC SQL INCLUDE SQLCA END-EXEC.

         01  DISPLAY-VARIABLES.
             05  D-PROD-NAME    PIC X(10).
             05  D-UPRICE      PIC Z(4)9.99.

 PROCEDURE DIVISION.

 BEGIN-PGM.
     EXEC SQL
         WHENEVER SQLERROR DO PERFORM SQL-ERROR
     END-EXEC.
     PERFORM LOGON.
     EXEC SQL
         DECLARE PRODUCTSNAME CURSOR FOR
         SELECT PROD_NAME, UNIT_PRICE FROM PRODUCTS
     END-EXEC.
     EXEC SQL
         OPEN PRODUCTSNAME
END-EXEC.
     DISPLAY "PRODUCT-NAME    UNIT-PRICE".
     DISPLAY "------------------------    -----------------".

 FETCH-LOOP.
     EXEC SQL
         WHENEVER NOT FOUND DO PERFORM SIGN-OFF
     END-EXEC.
     EXEC SQL
         FETCH PRODUCTSNAME
         INTO :PROD-NAME, :UPRICE
     END-EXEC.
     MOVE PROD-NAME-ARR TO D-PROD-NAME.
     MOVE UPRICE TO D-UPRICE.
      DISPLAY D-PROD-NAME, "     ", D-UPRICE.
     MOVE SPACES TO PROD-NAME-ARR.
     GO TO FETCH-LOOP.

 LOGON.
     MOVE "SCOTT" TO USERNAME-ARR.
     MOVE 5 TO USERNAME-LEN.
     MOVE "TIGER" TO PASSWD-ARR.
     MOVE 5 TO PASSWD-LEN.
     EXEC SQL
         CONNECT :USERNAME IDENTIFIED BY :PASSWD
     END-EXEC.
     DISPLAY " ".
     DISPLAY "CONNECTED TO ORACLE AS USER: ", USERNAME-ARR.
     DISPLAY " ".

 SIGN-OFF.
     EXEC SQL
         CLOSE PRODUCTSNAME
     END-EXEC.
     DISPLAY " ".
     EXEC SQL
         COMMIT WORK RELEASE
     END-EXEC.
     STOP RUN.

 SQL-ERROR.
     EXEC SQL
         WHENEVER SQLERROR CONTINUE
     END-EXEC.
     DISPLAY " ".
     DISPLAY "ORACLE ERROR DETECTED:".
     DISPLAY " ".
     DISPLAY SQLERRMC.
     EXEC SQL
         ROLLBACK WORK RELEASE
     END-EXEC.
     STOP RUN.
