HP OpenVMS Systems Documentation

Content starts here

HP COBOL
User Manual


Previous Contents Index

Rewriting relative records in random access mode involves the following:

  1. Specifying ORGANIZATION IS RELATIVE in the Environment Division SELECT clause
  2. Specifying ACCESS MODE IS RANDOM (or DYNAMIC) in the Environment Division SELECT clause
  3. Opening the file for I-O
  4. Moving the relative record number value of the record you want to read to the RELATIVE KEY data name
  5. Reading the record from the cell identified by the relative record number
  6. Updating the record
  7. Rewriting the record into the cell identified by the relative record number

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.

6.5.2.2 Deleting Records from a Relative File

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:

  • Sequential access mode deletion
  • Random access mode deletion

Deleting a Relative Record in Sequential Access Mode

Deleting a relative record in sequential access mode involves the following:

  1. Specifying ORGANIZATION IS RELATIVE in the Environment Division SELECT clause
  2. Specifying ACCESS MODE IS SEQUENTIAL in the Environment Division SELECT clause
  3. Opening the file for I-O
  4. Using a START statement to position the record pointer, or sequentially reading the file up to the target record
  5. Deleting the last read record

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:

  1. Specifing ORGANIZATION IS RELATIVE 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. Moving the relative record number value to the RELATIVE KEY data name
  5. Deleting the record identified by the relative record number

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.

6.5.3 Updating an Indexed File

Updating a record in an indexed file in sequential access mode involves the following:

  1. Reading the target record
  2. Verifying that the record is the one you want to change
  3. Changing the record
  4. Rewriting or deleting the target record

A program updates an indexed file in random access mode by rewriting or deleting the record.

Three options are available for updating indexed records:

  • Sequential access mode updating
  • Random access mode updating
  • Dynamic access mode updating

Note

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:

  1. Specifying ORGANIZATION IS INDEXED in the Environment Division SELECT clause
  2. Specifying ACCESS MODE IS SEQUENTIAL in Environment Division SELECT clause
  3. Opening the file for I-O
  4. Reading records as you would a sequential file (use the READ statement with the AT END phrase)
  5. Rewriting or deleting records using the INVALID KEY phrase

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