Previous | Contents | Index |
A program updates a relative file with the WRITE, REWRITE, and DELETE
statements. The WRITE statement adds a record to the file. Only the
REWRITE and DELETE statements change the contents of records already
existing in the file. In either case, adequate backup must be available
in the event of error. Sections 6.5.2.1 and 6.5.2.2 explain
how to rewrite and delete relative records, respectively.
6.5.2.1 Rewriting a Relative File
The REWRITE statement logically replaces a record in a relative file; the original contents of the record are lost. Two options are available for rewriting relative records:
Rewriting Relative Records in Sequential Access Mode
Rewriting relative records in sequential access mode involves the following:
Example 6-38 reads a relative record sequentially and displays the record on the terminal. The program then passes the record to an update routine that is not included in the example. The update routine updates the record, and passes the updated record back to the program illustrated in Example 6-38, which displays the updated record on the terminal and rewrites the record in the same cell.
Example 6-38 Rewriting Relative Records in Sequential Access Mode |
---|
IDENTIFICATION DIVISION. PROGRAM-ID. REL07. 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 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 (ZERO to END)". ACCEPT KETCHUP-MASTER-KEY WITH CONVERSION. IF KETCHUP-MASTER-KEY IS NOT EQUAL TO 00 START FLAVORS KEY IS EQUAL TO KETCHUP-MASTER-KEY INVALID KEY DISPLAY "BAD START" STOP RUN. END-START PERFORM A200-READ-FLAVORS DISPLAY "*********BEFORE UPDATE*********" DISPLAY KETCHUP-MASTER ************************************************************ * * Update routine code here * ************************************************************ DISPLAY "*********AFTER UPDATE*********" DISPLAY KETCHUP-MASTER REWRITE KETCHUP-MASTER. A200-READ-FLAVORS. READ FLAVORS AT END DISPLAY "END OF FILE" GO TO A005-EOJ. |
Rewriting Relative Records in Random Access Mode
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-39 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-39 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-40 deletes relative records in sequential access mode.
Example 6-40 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 Relative Records 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-41 deletes relative records in random access mode.
Example 6-41 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-42 updates an indexed file sequentially.
Example 6-42 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
Updating indexed records in random access mode involves the following:
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-43 updates an indexed file randomly.
Example 6-43 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:
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:
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.
Previous | Next | Contents | Index |