IDENTIFICATION DIVISION.
 PROGRAM-ID. QUERY.
 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  PRODUCT-ID     PIC S9(4) COMP VALUE ZERO.
             05  UNIT-PRICE         PIC S9(5)V99 COMP-3 VALUE ZERO.

     EXEC SQL END DECLARE SECTION END-EXEC.

     EXEC SQL INCLUDE SQLCA END-EXEC.

         01  DISPLAY-VARIABLES.
             05  D-PRODUCT-NAME     PIC X(20).
             05  D-UNIT-PRICE       PIC Z(4)9.99.

         01  D-TOTAL-QUERIED    PIC 9(4) VALUE ZERO.

 PROCEDURE DIVISION.
 BEGIN-PGM.
     EXEC SQL
         WHENEVER SQLERROR DO PERFORM SQL-ERROR
     END-EXEC.
     PERFORM LOGON.

 QUERY-LOOP.
     DISPLAY " ".
     DISPLAY "ENTER PRODUCT ID TO SEARCH (0 TO QUIT): " WITH NO ADVANCING.
     ACCEPT PRODUCT-ID.
     IF (PRODUCT-ID = 0) PERFORM SIGN-OFF.
     MOVE SPACES TO PRODUCT-NAME-ARR.
     EXEC SQL
         WHENEVER NOT FOUND GOTO NO-PROD
     END-EXEC.
     EXEC SQL
         SELECT PRODNAME,UPRICE
         INTO :PRODUCT-NAME, :UNIT-PRICE
         FROM PRODUCTS
         WHERE PRODID = :PRODUCT-ID
     END-EXEC.
     PERFORM DISPLAY-INFO.
     ADD 1 TO D-TOTAL-QUERIED.
     GO TO QUERY-LOOP.

 NO-PROD.
     DISPLAY "NOT A VALID PRODUCT ID - TRY AGAIN.".
     GO TO QUERY-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-INFO.
     DISPLAY " ".
     DISPLAY "PRODUCT UNIT-PRICE".
     DISPLAY "--------------  ----------------".
     MOVE PRODUCT-NAME-ARR TO D-PRODUCT-NAME.
     MOVE UNIT-PRICE TO D-UNIT-PRICE.

 SIGN-OFF.
     DISPLAY " ".
     DISPLAY "TOTAL NUMBER QUERIED WAS ", D-TOTAL-QUERIED, ".".
     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.
