HP OpenVMS Systems Documentation
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.
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
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.
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:
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.