HP OpenVMS Systems Documentation |
HP COBOL
|
Previous | Contents | Index |
Rewriting relative records in random access mode involves the following:
During execution of the REWRITE statement, the I/O system randomly reads the record identified by the RELATIVE KEY IS clause. The REWRITE statement then places the successfully read record back into its cell in the file.
If the cell does not contain a valid record, or if the REWRITE operation is unsuccessful, the invalid key condition occurs, and the REWRITE operation fails (see Chapter 7).
Example 6-40 reads a relative record randomly, displays its contents on the terminal, updates the record, displays its updated contents on the terminal, and rewrites the record in the same cell.
Example 6-40 Rewriting Relative Records in Random Access Mode |
---|
IDENTIFICATION DIVISION. PROGRAM-ID. REL08. ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT FLAVORS ASSIGN TO "BRAND" ORGANIZATION IS RELATIVE ACCESS MODE IS RANDOM RELATIVE KEY IS KETCHUP-MASTER-KEY. DATA DIVISION. FILE SECTION. FD FLAVORS. 01 KETCHUP-MASTER PIC X(50). WORKING-STORAGE SECTION. 01 KETCHUP-MASTER-KEY PIC 99. PROCEDURE DIVISION. A000-BEGIN. OPEN I-O FLAVORS. PERFORM A100-UPDATE-RECORD UNTIL KETCHUP-MASTER-KEY = 00. A005-EOJ. DISPLAY "END OF JOB". CLOSE FLAVORS. STOP RUN. A100-UPDATE-RECORD. DISPLAY "TO UPDATE A RECORD ENTER ITS RECORD NUMBER". ACCEPT KETCHUP-MASTER-KEY. READ FLAVORS INVALID KEY DISPLAY "BAD READ" GO TO A005-EOJ. DISPLAY "*********BEFORE UPDATE*********". DISPLAY KETCHUP-MASTER. ******************************************************** * * Update routine * ******************************************************** DISPLAY "*********AFTER UPDATE*********". DISPLAY KETCHUP-MASTER. REWRITE KETCHUP-MASTER INVALID KEY DISPLAY "BAD REWRITE" GO TO A005-EOJ. |
The DELETE statement logically removes an existing record from a relative file. After successfully removing a record from a file, the program cannot later access it. Two options are available for deleting relative records:
Deleting a Relative Record in Sequential Access Mode
Deleting a relative record in sequential access mode involves the following:
Example 6-41 deletes relative records in sequential access mode.
Example 6-41 Deleting Relative Records in Sequential Access Mode |
---|
IDENTIFICATION DIVISION. PROGRAM-ID. REL09. ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT FLAVORS ASSIGN TO "BRAND" ORGANIZATION IS RELATIVE ACCESS MODE IS SEQUENTIAL RELATIVE KEY IS KETCHUP-MASTER-KEY. DATA DIVISION. FILE SECTION. FD FLAVORS. 01 KETCHUP-MASTER PIC X(50). WORKING-STORAGE SECTION. 01 KETCHUP-MASTER-KEY PIC 99 VALUE 1. PROCEDURE DIVISION. A000-BEGIN. OPEN I-O FLAVORS. PERFORM A010-DELETE-RECORDS UNTIL KETCHUP-MASTER-KEY = 00. A005-EOJ. DISPLAY "END OF JOB". CLOSE FLAVORS. STOP RUN. A010-DELETE-RECORDS. DISPLAY "TO DELETE A RECORD ENTER ITS RECORD NUMBER". ACCEPT KETCHUP-MASTER-KEY. IF KETCHUP-MASTER-KEY NOT = 00 PERFORM A200-READ-FLAVORS DELETE FLAVORS RECORD. A200-READ-FLAVORS. START FLAVORS INVALID KEY DISPLAY "INVALID START" STOP RUN. READ FLAVORS AT END DISPLAY "FILE AT END" GO TO A005-EOJ. |
Deleting a Relative Record in Random Access Mode
Deleting a relative record in random access mode involves the following:
If the file does not contain a valid record, an invalid key condition exists.
Example 6-42 deletes relative records in random access mode.
Example 6-42 Deleting Relative Records in Random Access Mode |
---|
IDENTIFICATION DIVISION. PROGRAM-ID. REL10. ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT FLAVORS ASSIGN TO "BRAND" ORGANIZATION IS RELATIVE ACCESS MODE IS RANDOM RELATIVE KEY IS KETCHUP-MASTER-KEY. DATA DIVISION. FILE SECTION. FD FLAVORS. 01 KETCHUP-MASTER PIC X(50). WORKING-STORAGE SECTION. 01 KETCHUP-MASTER-KEY PIC 99 VALUE 1. PROCEDURE DIVISION. A000-BEGIN. OPEN I-O FLAVORS. PERFORM A010-DELETE-RECORDS UNTIL KETCHUP-MASTER-KEY = 00. A005-EOJ. DISPLAY "END OF JOB". CLOSE FLAVORS. STOP RUN. A010-DELETE-RECORDS. DISPLAY "TO DELETE A RECORD ENTER ITS RECORD NUMBER". ACCEPT KETCHUP-MASTER-KEY. IF KETCHUP-MASTER-KEY NOT = 00 DELETE FLAVORS RECORD INVALID KEY DISPLAY "INVALID DELETE" STOP RUN. |
Updating a record in an indexed file in sequential access mode involves the following:
A program updates an indexed file in random access mode by rewriting or deleting the record.
Three options are available for updating indexed records:
A program cannot rewrite an existing record if it changes the contents of the primary key in that record. Instead, the program must delete the record and write a new record. Alternate key values can be changed at any time. However, the value of alternate keys must be unique unless the WITH DUPLICATES phrase is present. |
Updating an Indexed File Sequentially
Updating indexed records in sequential acess mode involves the following:
The READ statement makes the next logical record of an open file available to the program. It skips deleted records and sequentially reads and retrieves only valid records. When the at end condition occurs, execution of the READ statement is unsuccessful (see Chapter 7).
The REWRITE statement replaces the record just read, while the DELETE statement logically removes the record just read from the file.
Example 6-43 updates an indexed file sequentially.
Example 6-43 Updating an Indexed File Sequentially |
---|
IDENTIFICATION DIVISION. PROGRAM-ID. INDEX06. ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT FLAVORS ASSIGN TO "DAIRY" ORGANIZATION IS INDEXED ACCESS MODE IS SEQUENTIAL 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 END-OF-FILE PIC X. 01 REWRITE-KEY PIC XXXXX. 01 DELETE-KEY PIC XX. 01 NEW-ADDRESS PIC X(20). PROCEDURE DIVISION. A000-BEGIN. OPEN I-O FLAVORS. DISPLAY "Which store code do you want to find?". ACCEPT REWRITE-KEY. DISPLAY "What is its new address?". ACCEPT NEW-ADDRESS. DISPLAY "Which state do you want to delete?". ACCEPT DELETE-KEY. PERFORM A100-READ-INPUT UNTIL END-OF-FILE = "Y". A020-EOJ. DISPLAY "END OF JOB". STOP RUN. A100-READ-INPUT. READ FLAVORS AT END MOVE "Y" TO END-OF-FILE. IF END-OF-FILE NOT = "Y" AND REWRITE-KEY = ICE-CREAM-STORE-CODE PERFORM A200-REWRITE-MASTER. IF END-OF-FILE NOT = "Y" AND DELETE-KEY = ICE-CREAM-STORE-STATE PERFORM A300-DELETE-MASTER. A200-REWRITE-MASTER. MOVE NEW-ADDRESS TO ICE-CREAM-STORE-ADDRESS. REWRITE ICE-CREAM-MASTER INVALID KEY DISPLAY "Bad rewrite - ABORTED" STOP RUN. A300-DELETE-MASTER. DELETE FLAVORS. |
Updating an Indexed File Randomly
Previous | Next | Contents | Index |