MainframeMaster
MainframeMaster

COBOL Tutorial

Progress0 of 0 lessons

COBOL Record Management

Record management in COBOL involves operations for reading, writing, updating, and deleting records in files. This includes defining record structures, opening and closing files, performing I/O operations (READ, WRITE, REWRITE, DELETE), handling file status codes, managing record keys for indexed files, and processing records sequentially or randomly. Understanding record management is essential for file processing, data manipulation, and building file-based applications in mainframe COBOL programs.

What is Record Management?

Record management encompasses all operations for working with file records:

  • Reading records: Retrieving records from files
  • Writing records: Adding new records to files
  • Updating records: Modifying existing records
  • Deleting records: Removing records from files
  • Record structure: Defining the layout of records
  • File operations: Opening, closing, and managing files
  • Error handling: Managing file status codes and errors

Effective record management ensures data integrity, proper file handling, and reliable file operations.

Defining Record Structures

Record structures define the layout of records in files:

Basic Record Definition

cobol
1
2
3
4
5
6
7
8
9
DATA DIVISION. FILE SECTION. FD CUSTOMER-FILE. 01 CUSTOMER-RECORD. 05 CUSTOMER-ID PIC 9(5). 05 CUSTOMER-NAME PIC X(30). 05 CUSTOMER-ADDRESS PIC X(50). 05 CUSTOMER-BALANCE PIC 9(8)V99. 05 FILLER PIC X(35). *> Padding to fixed length

The record structure defines:

  • Field names: Identifiers for each data element
  • Data types: PIC clauses specifying format and size
  • Field positions: Order and position within the record
  • Record length: Total size of the record

Hierarchical Record Structures

cobol
1
2
3
4
5
6
7
8
9
10
11
12
FD EMPLOYEE-FILE. 01 EMPLOYEE-RECORD. 05 EMPLOYEE-ID PIC 9(6). 05 EMPLOYEE-NAME. 10 FIRST-NAME PIC X(15). 10 LAST-NAME PIC X(15). 05 EMPLOYEE-ADDRESS. 10 STREET PIC X(30). 10 CITY PIC X(20). 10 STATE PIC X(2). 10 ZIP-CODE PIC 9(5). 05 EMPLOYEE-SALARY PIC 9(7)V99.

Hierarchical structures organize related fields into groups, improving readability and organization.

Opening and Closing Files

Files must be opened before use and closed when done:

File Open Modes

cobol
1
2
3
4
5
6
7
8
9
10
11
*> INPUT - Read only OPEN INPUT CUSTOMER-FILE *> OUTPUT - Write only (creates new file, deletes existing) OPEN OUTPUT CUSTOMER-FILE *> I-O - Input-Output (read and write, for updates) OPEN I-O CUSTOMER-FILE *> EXTEND - Append to existing file OPEN EXTEND CUSTOMER-FILE

Choose the appropriate mode based on your operations:

  • INPUT: When you only need to read records
  • OUTPUT: When creating a new file (deletes existing file)
  • I-O: When you need to read, update, or delete records
  • EXTEND: When adding records to an existing file

Closing Files

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
*> Close single file CLOSE CUSTOMER-FILE *> Close multiple files CLOSE CUSTOMER-FILE EMPLOYEE-FILE TRANSACTION-FILE *> Close with error handling CLOSE CUSTOMER-FILE IF CUSTOMER-STATUS NOT = '00' DISPLAY "Error closing file: " CUSTOMER-STATUS END-IF

Always close files when done to ensure data is written and resources are released.

Reading Records

Reading records depends on file organization and access mode:

Sequential File Reading

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
PROCEDURE DIVISION. MAIN-PARA. OPEN INPUT CUSTOMER-FILE PERFORM UNTIL END-OF-FILE READ CUSTOMER-FILE AT END SET END-OF-FILE TO TRUE DISPLAY "End of file reached" NOT AT END DISPLAY "Customer: " CUSTOMER-NAME DISPLAY "Balance: " CUSTOMER-BALANCE *> Process record END-READ END-PERFORM CLOSE CUSTOMER-FILE STOP RUN.

Sequential reading processes records one after another in the order they appear in the file.

Random Access Reading (Indexed Files)

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
WORKING-STORAGE SECTION. 01 SEARCH-CUSTOMER-ID PIC 9(5). PROCEDURE DIVISION. MAIN-PARA. OPEN INPUT CUSTOMER-FILE *> Set key for random access MOVE 12345 TO CUSTOMER-ID READ CUSTOMER-FILE INVALID KEY DISPLAY "Customer not found: " CUSTOMER-ID NOT INVALID KEY DISPLAY "Customer found: " CUSTOMER-NAME DISPLAY "Balance: " CUSTOMER-BALANCE END-READ CLOSE CUSTOMER-FILE STOP RUN.

For random access, set the RECORD KEY before reading. The key must match an existing record.

Sequential Reading (Indexed Files)

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
PROCEDURE DIVISION. MAIN-PARA. OPEN INPUT CUSTOMER-FILE *> Read records in key order PERFORM UNTIL END-OF-FILE READ CUSTOMER-FILE NEXT AT END SET END-OF-FILE TO TRUE NOT AT END DISPLAY "Customer ID: " CUSTOMER-ID DISPLAY "Name: " CUSTOMER-NAME END-READ END-PERFORM CLOSE CUSTOMER-FILE STOP RUN.

READ NEXT reads records sequentially in key order for indexed files.

Writing Records

Writing records adds new records to files:

Writing to Sequential Files

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
PROCEDURE DIVISION. MAIN-PARA. OPEN OUTPUT CUSTOMER-FILE *> Prepare record data MOVE 12345 TO CUSTOMER-ID MOVE 'JOHN SMITH' TO CUSTOMER-NAME MOVE '123 MAIN ST' TO CUSTOMER-ADDRESS MOVE 1000.50 TO CUSTOMER-BALANCE *> Write record WRITE CUSTOMER-RECORD *> Or write from another field MOVE WS-CUSTOMER-DATA TO CUSTOMER-RECORD WRITE CUSTOMER-RECORD CLOSE CUSTOMER-FILE STOP RUN.

Writing to Indexed Files

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
PROCEDURE DIVISION. MAIN-PARA. OPEN OUTPUT CUSTOMER-FILE *> Set key and record data MOVE 12345 TO CUSTOMER-ID MOVE 'JOHN SMITH' TO CUSTOMER-NAME MOVE 1000.50 TO CUSTOMER-BALANCE *> Write record WRITE CUSTOMER-RECORD INVALID KEY DISPLAY "Error: Duplicate key or invalid key" DISPLAY "Customer ID: " CUSTOMER-ID NOT INVALID KEY DISPLAY "Record written successfully" END-WRITE CLOSE CUSTOMER-FILE STOP RUN.

For indexed files, set the RECORD KEY before writing. INVALID KEY handles duplicate keys or invalid key values.

Updating Records

Updating records requires reading first, then rewriting:

Update Process

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
PROCEDURE DIVISION. MAIN-PARA. OPEN I-O CUSTOMER-FILE *> Step 1: Read the record to update MOVE 12345 TO CUSTOMER-ID READ CUSTOMER-FILE INVALID KEY DISPLAY "Customer not found: " CUSTOMER-ID STOP RUN NOT INVALID KEY *> Step 2: Modify the record data ADD 100.00 TO CUSTOMER-BALANCE MOVE 'UPDATED ADDRESS' TO CUSTOMER-ADDRESS *> Step 3: Write the updated record back REWRITE CUSTOMER-RECORD INVALID KEY DISPLAY "Error updating record" NOT INVALID KEY DISPLAY "Record updated successfully" END-REWRITE END-READ CLOSE CUSTOMER-FILE STOP RUN.

The update process:

  • 1. Read the record: Retrieve the record you want to update
  • 2. Modify data: Change field values in memory
  • 3. REWRITE: Write the updated record back to the file

Important: For indexed files, you cannot change the RECORD KEY when updating. The key must remain the same.

Deleting Records

Deleting records removes them from the file:

Deleting from Indexed Files

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
PROCEDURE DIVISION. MAIN-PARA. OPEN I-O CUSTOMER-FILE *> Set key of record to delete MOVE 12345 TO CUSTOMER-ID *> Delete the record DELETE CUSTOMER-FILE RECORD INVALID KEY DISPLAY "Record not found: " CUSTOMER-ID NOT INVALID KEY DISPLAY "Record deleted successfully" END-DELETE CLOSE CUSTOMER-FILE STOP RUN.

DELETE removes the record with the specified key from the file. The record is permanently removed and cannot be recovered.

Deleting from Relative Files

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
WORKING-STORAGE SECTION. 01 RECORD-NUMBER PIC 9(4). PROCEDURE DIVISION. MAIN-PARA. OPEN I-O LOOKUP-TABLE *> Set relative record number MOVE 10 TO RECORD-NUMBER *> Delete record at that position DELETE LOOKUP-TABLE RECORD INVALID KEY DISPLAY "Record not found at position: " RECORD-NUMBER END-DELETE CLOSE LOOKUP-TABLE STOP RUN.

For relative files, set the RELATIVE KEY (record number) before deleting.

File Status Codes

File status codes indicate the result of file operations:

Common File Status Codes
Status CodeMeaningDescription
'00'SuccessOperation completed successfully
'10'End of fileNo more records available (sequential read)
'23'Record not foundRecord with specified key not found (random access)
'30'Permanent I/O errorHardware or file system error
'22'Duplicate keyAttempt to write duplicate key (indexed files)
'24'Boundary violationAttempt to write beyond file limits

Using File Status

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT CUSTOMER-FILE ASSIGN TO CUSTOMER.DAT FILE STATUS IS CUSTOMER-STATUS. WORKING-STORAGE SECTION. 01 CUSTOMER-STATUS PIC XX. 88 FILE-OK VALUE '00'. 88 END-OF-FILE VALUE '10'. 88 RECORD-NOT-FOUND VALUE '23'. 88 FILE-ERROR VALUE '30'. PROCEDURE DIVISION. MAIN-PARA. OPEN INPUT CUSTOMER-FILE IF NOT FILE-OK EVALUATE CUSTOMER-STATUS WHEN '30' DISPLAY "Permanent I/O error" WHEN OTHER DISPLAY "File open error: " CUSTOMER-STATUS END-EVALUATE STOP RUN END-IF *> Process file... CLOSE CUSTOMER-FILE STOP RUN.

Record Processing Patterns

Pattern 1: Sequential Processing

cobol
1
2
3
4
5
6
7
8
9
10
*> Process all records sequentially OPEN INPUT INPUT-FILE PERFORM UNTIL END-OF-FILE READ INPUT-FILE AT END SET END-OF-FILE TO TRUE NOT AT END PERFORM PROCESS-RECORD END-READ END-PERFORM CLOSE INPUT-FILE

Pattern 2: Lookup and Update

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
*> Look up record and update MOVE SEARCH-KEY TO RECORD-KEY READ FILE-NAME INVALID KEY DISPLAY "Record not found" NOT INVALID KEY *> Modify record ADD AMOUNT TO BALANCE-FIELD REWRITE RECORD-NAME INVALID KEY DISPLAY "Update failed" END-REWRITE END-READ

Pattern 3: Add New Record

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
*> Add new record with validation MOVE NEW-KEY TO RECORD-KEY MOVE NEW-DATA TO RECORD-FIELDS WRITE RECORD-NAME INVALID KEY IF FILE-STATUS = '22' DISPLAY "Duplicate key - record exists" ELSE DISPLAY "Write error: " FILE-STATUS END-IF NOT INVALID KEY DISPLAY "Record added successfully" END-WRITE

Best Practices for Record Management

Follow these best practices:

  • Always check file status: Check FILE STATUS after every file operation
  • Handle end of file: Use AT END for sequential reads to detect end of file
  • Use INVALID KEY: Handle INVALID KEY for random access operations
  • Close files properly: Always close files when done, even on errors
  • Validate before write: Ensure data is valid before writing records
  • Read before update: Always read a record before updating it
  • Set keys correctly: Ensure RECORD KEY or RELATIVE KEY is set before random operations
  • Handle errors gracefully: Provide meaningful error messages and recovery options
  • Use appropriate open mode: Choose INPUT, OUTPUT, I-O, or EXTEND based on operations needed
  • Document record structure: Comment record definitions to explain field purposes

Common Mistakes in Record Management

Avoid these common mistakes:

Mistake 1: Not Checking File Status

cobol
1
2
3
4
5
6
7
8
9
10
11
*> WRONG: No error checking READ CUSTOMER-FILE DISPLAY CUSTOMER-NAME *> CORRECT: Check file status READ CUSTOMER-FILE AT END DISPLAY "End of file" NOT AT END DISPLAY CUSTOMER-NAME END-READ

Mistake 2: Forgetting to Set Key

cobol
1
2
3
4
5
6
7
8
9
*> WRONG: Key not set READ CUSTOMER-FILE INVALID KEY... *> CORRECT: Set key first MOVE 12345 TO CUSTOMER-ID READ CUSTOMER-FILE INVALID KEY... END-READ

Mistake 3: Updating Without Reading

cobol
1
2
3
4
5
6
7
8
9
10
*> WRONG: REWRITE without READ MOVE NEW-DATA TO CUSTOMER-RECORD REWRITE CUSTOMER-RECORD *> CORRECT: Read first, then REWRITE READ CUSTOMER-FILE NOT INVALID KEY MOVE NEW-DATA TO CUSTOMER-RECORD REWRITE CUSTOMER-RECORD END-READ

Explain Like I'm 5: Record Management

Think of record management like managing a filing cabinet:

  • Opening a file is like opening the filing cabinet drawer - you need to open it before you can use it
  • Reading a record is like taking a file out of the cabinet to look at it
  • Writing a record is like putting a new file into the cabinet
  • Updating a record is like taking out a file, changing something in it, and putting it back
  • Deleting a record is like throwing away a file from the cabinet
  • Closing a file is like closing the drawer when you're done - you always close it when finished

So record management is like organizing and managing files in a filing cabinet - you open it, work with files, and close it when done!

Practice Exercises

Complete these exercises to reinforce your understanding:

Exercise 1: Sequential Reading

Create a program that opens a sequential file, reads all records, and displays each record's key information. Handle end of file appropriately.

Exercise 2: Random Access Lookup

Create a program that opens an indexed file, accepts a key value, and looks up the record. Display the record if found, or an error message if not found.

Exercise 3: Record Update

Create a program that reads a record from an indexed file, modifies specific fields, and uses REWRITE to update the record. Include proper error handling.

Exercise 4: Add New Record

Create a program that accepts input for a new record, sets the key and data fields, and writes the record to an indexed file. Handle duplicate key errors.

Exercise 5: Delete Record

Create a program that accepts a key value, deletes the corresponding record from an indexed file, and handles cases where the record is not found.

Test Your Knowledge

1. What statement is used to update an existing record?

  • UPDATE
  • REWRITE
  • MODIFY
  • CHANGE

2. What must you do before reading a record with random access?

  • Open the file
  • Set the RECORD KEY
  • Set file status
  • Initialize the record

3. What file status code indicates end of file?

  • '00'
  • '10'
  • '23'
  • '30'

4. What file status code indicates record not found?

  • '00'
  • '10'
  • '23'
  • '30'

5. What is the correct sequence for updating a record?

  • REWRITE, then READ
  • READ, modify data, then REWRITE
  • WRITE, then READ
  • DELETE, then WRITE

6. Which file organization does NOT support DELETE?

  • KSDS
  • ESDS
  • RRDS
  • All support DELETE

Related Concepts

Related Pages