HP OpenVMS Systems Documentation

Content starts here

HP COBOL
User Manual


Previous Contents Index

Updating indexed records in random access mode involves the following:

  1. Specifying ORGANIZATION IS INDEXED in the Environment Division SELECT clause
  2. Specifying ACCESS MODE IS RANDOM in the Environment Division SELECT clause
  3. Opening the file for I-O
  4. Initializing the RECORD KEY or ALTERNATE RECORD KEY data name
  5. Writing, rewriting, or deleting records using the INVALID KEY phrase

You do not need to first read a record to update or delete it. If the primary or alternate key you specify allows duplicates, only the first occurrence of a record with a matching value will be updated.

Example 6-44 updates an indexed file randomly.

Example 6-44 Updating an Indexed File Randomly

IDENTIFICATION DIVISION.
PROGRAM-ID. INDEX07.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
    SELECT FLAVORS    ASSIGN TO "DAIRY"
                      ORGANIZATION IS INDEXED
                      ACCESS MODE IS RANDOM
                      RECORD KEY IS ICE-CREAM-MASTER-KEY
                      ALTERNATE RECORD KEY IS ICE-CREAM-STORE-STATE
                                           WITH DUPLICATES
                      ALTERNATE RECORD KEY IS ICE-CREAM-STORE-CODE.
DATA DIVISION.
FILE SECTION.
FD  FLAVORS.
01  ICE-CREAM-MASTER.
    02 ICE-CREAM-MASTER-KEY          PIC XXXX.
    02 ICE-CREAM-MASTER-DATA.
       03  ICE-CREAM-STORE-CODE      PIC XXXXX.
       03  ICE-CREAM-STORE-ADDRESS   PIC X(20).
       03  ICE-CREAM-STORE-CITY      PIC X(20).
       03  ICE-CREAM-STORE-STATE     PIC XX.
WORKING-STORAGE SECTION.
01  HOLD-ICE-CREAM-MASTER            PIC X(51).
01  PROGRAM-STAT                     PIC X.
    88  OPERATOR-STOPS-IT            VALUE "1".
    88  LETS-SEE-NEXT-STORE          VALUE "2".
    88  NO-MORE-DUPLICATES           VALUE "3".
PROCEDURE DIVISION.
A000-BEGIN.
    OPEN I-O FLAVORS.
    PERFORM A030-RANDOM-READ UNTIL OPERATOR-STOPS-IT.
A020-EOJ.
    DISPLAY "END OF JOB".
    STOP RUN.
A030-RANDOM-READ.
    DISPLAY "Enter key".
    ACCEPT ICE-CREAM-MASTER-KEY.
    PERFORM A100-READ-INPUT-BY-PRIMARY-KEY
            THROUGH A100-READ-INPUT-EXIT.
    DISPLAY " Do you want to terminate the session?".
    PERFORM A040-GET-ANSWER UNTIL PROGRAM-STAT   = "Y" OR "N".
    IF PROGRAM-STAT   = "Y" MOVE "1" TO PROGRAM-STAT.
A040-GET-ANSWER.
        DISPLAY "Please answer Y or N"
        ACCEPT PROGRAM-STAT.
A100-READ-INPUT-BY-PRIMARY-KEY.
    READ FLAVORS KEY IS ICE-CREAM-MASTER-KEY
         INVALID KEY DISPLAY "Master does not exist - Try again"
         GO TO A100-READ-INPUT-EXIT.
    DISPLAY ICE-CREAM-MASTER.
    PERFORM A200-READ-BY-ALTERNATE-KEY UNTIL NO-MORE-DUPLICATES.
A100-READ-INPUT-EXIT.
    EXIT.
A200-READ-BY-ALTERNATE-KEY.
    DISPLAY "Do you want to see the next store in this state?".
    PERFORM A040-GET-ANSWER UNTIL PROGRAM-STAT   = "Y" OR "N".
    IF PROGRAM-STAT   = "Y"
       MOVE "2" TO PROGRAM-STAT
       READ FLAVORS KEY IS ICE-CREAM-STORE-STATE
                    INVALID KEY DISPLAY "No more stores in this state"
                                MOVE "3" TO PROGRAM-STAT.
    IF LETS-SEE-NEXT-STORE AND
       ICE-CREAM-STORE-STATE = "NY"
             PERFORM A500-DELETE-RANDOM-RECORD.
    IF LETS-SEE-NEXT-STORE AND
       ICE-CREAM-STORE-STATE = "NJ"
             MOVE "Monmouth" TO ICE-CREAM-STORE-CITY
             PERFORM A400-REWRITE-RANDOM-RECORD.
    IF LETS-SEE-NEXT-STORE AND
       ICE-CREAM-STORE-STATE = "CA"
             MOVE ICE-CREAM-MASTER TO HOLD-ICE-CREAM-MASTER
             PERFORM A500-DELETE-RANDOM-RECORD
             MOVE HOLD-ICE-CREAM-MASTER TO ICE-CREAM-MASTER
             MOVE "AZ" TO ICE-CREAM-STORE-STATE
             PERFORM A300-WRITE-RANDOM-RECORD.
    IF PROGRAM-STAT   = "N"
       MOVE "3" TO PROGRAM-STAT.
A300-WRITE-RANDOM-RECORD.
    WRITE ICE-CREAM-MASTER
          INVALID KEY DISPLAY "Bad write - ABORTED"
                      STOP RUN.
A400-REWRITE-RANDOM-RECORD.
    REWRITE ICE-CREAM-MASTER
            INVALID KEY DISPLAY "Bad rewrite - ABORTED"
                        STOP RUN.
A500-DELETE-RANDOM-RECORD.
    DELETE FLAVORS
           INVALID KEY DISPLAY "Bad delete - ABORTED"
                       STOP RUN.

Updating an Indexed File Dynamically

Updating indexed records in dynamic access mode involves the following:

  1. Specifying ORGANIZATION IS INDEXED in the Environment Division SELECT clause
  2. Specifying ACCESS MODE IS DYNAMIC in the Environment Division SELECT clause
  3. Opening the file for I-O
  4. Reading the records sequentially (using the START statement to position the record pointer and then using the READ...NEXT statement) or randomly (initializing the RECORD KEY or ALTERNATE RECORD KEY data name and then reading records in any order you want using the INVALID KEY phrase) (See Example 6-44.)
  5. Rewriting or deleting records using the INVALID KEY phrase

For indexed files with duplicate primary keys values, rewriting and deleting work as if the file was opened in sequential access mode. You first read the record, then update or delete the record just read.

For indexed files without duplicates allowed on the primary key, rewriting and deleting work as if the file was opened in random access mode. Specify the value of the primary key data item to indicate the target record, then update or delete that record.

In dynamic access mode, the program can switch from using random access I/O statements to sequential access I/O statements in any order without closing and reopening files.

6.6 Backing Up Your Files

Files can become unusable if either of the following situations occur:

  • Your disk file becomes corrupted by a hardware error.
  • Your disk file becomes corrupted with bad data.

Proper backup procedures are the key to successful recovery. You should back up your disk file at some reasonable point (daily, weekly, or monthly, depending on file activity and value of data), and save all transactions until you create a new backup. In this way, you can easily recreate your disk file from your last backup file and transaction files whenever the need arises.


Chapter 7
Handling Input/Output Exception Conditions

Many types of exception conditions can occur when a program processes a file; not all of them are errors. The three categories of exception conditions are as follows:

  • AT END condition---This is a normal condition when you access a file sequentially. However, if your program tries to read the file any time after having read the last logical record in the file, and there is no applicable Declarative USE procedure or AT END phrase, the program abnormally terminates when the next READ statement executes.
  • Invalid key condition---When you process relative and indexed files, the invalid key condition is a normal condition if you plan for it with a Declarative USE procedure or INVALID KEY phrase. It is an abnormal condition that causes your program to terminate if there is no applicable Declarative USE procedure or INVALID KEY phrase.
  • All other conditions---These can also be either normal conditions (if you plan for them with Declarative USE procedures) or abnormal conditions that cause your program to terminate.

Planning for exception conditions effectively increases program and programmer efficiency. A program with exception handling routines is more flexible than a program without them. Exception handling routines minimize operator intervention and often reduce or eliminate the time you need to spend debugging and rerunning your program.

This chapter introduces you to the tools you need to execute exception handling routines for sequential, relative, and indexed files as a normal part of your program. These tools are the AT END phrase, the INVALID KEY phrase, file status values, RMS completion codes (on OpenVMS systems), and Declarative USE procedures. The topics that follow explain how to use these tools in your programs:

7.1 Planning for the AT END Condition

HP COBOL provides you the option of testing for this condition with the AT END phrase of the READ statement (for sequential, relative, and indexed files) and the AT END phrase of the ACCEPT statement.

Programs often read sequential files from beginning to end. They can produce reports from the information in the file or even update it. However, the program must be able to detect the end of the file, so that it can continue normal processing at that point. If the program does not test for this condition when it occurs, and if no applicable Declarative USE procedure exists (see Section 7.4), the program terminates abnormally. The program must detect when no more data is available from the file so that it can perform its normal end-of-job processing and then close the file.

Example 7-1 shows the use of the AT END phrase with the READ statement for sequential, relative, and indexed files.

Example 7-1 Handling the AT END Condition

READ SEQUENTIAL-FILE AT END PERFORM A600-TOTAL-ROUTINES
                            PERFORM A610-VERIFY-TOTALS-ROUTINES
                            MOVE "Y" TO END-OF-FILE.
READ RELATIVE-FILE NEXT RECORD AT END PERFORM A700-CLEAN-UP-ROUTINES
                                      CLOSE RELATIVE-FILE
                                      STOP RUN.
READ INDEXED-FILE NEXT RECORD AT END DISPLAY "End of file"
                                     DISPLAY "Do you want to continue?"
                                     ACCEPT REPLY
                                     PERFORM A700-CLEAN-UP-ROUTINES.


Previous Next Contents Index