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.
Record management encompasses all operations for working with file records:
Effective record management ensures data integrity, proper file handling, and reliable file operations.
Record structures define the layout of records in files:
123456789DATA 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:
123456789101112FD 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.
Files must be opened before use and closed when done:
1234567891011*> 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:
12345678910111213*> 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 depends on file organization and access mode:
123456789101112131415161718PROCEDURE 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.
12345678910111213141516171819WORKING-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.
1234567891011121314151617PROCEDURE 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 adds new records to files:
12345678910111213141516171819PROCEDURE 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.
1234567891011121314151617181920PROCEDURE 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 requires reading first, then rewriting:
1234567891011121314151617181920212223242526PROCEDURE 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:
Important: For indexed files, you cannot change the RECORD KEY when updating. The key must remain the same.
Deleting records removes them from the file:
1234567891011121314151617PROCEDURE 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.
123456789101112131415161718WORKING-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 indicate the result of file operations:
| Status Code | Meaning | Description |
|---|---|---|
| '00' | Success | Operation completed successfully |
| '10' | End of file | No more records available (sequential read) |
| '23' | Record not found | Record with specified key not found (random access) |
| '30' | Permanent I/O error | Hardware or file system error |
| '22' | Duplicate key | Attempt to write duplicate key (indexed files) |
| '24' | Boundary violation | Attempt to write beyond file limits |
123456789101112131415161718192021222324252627282930ENVIRONMENT 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.
12345678910*> 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
12345678910111213*> 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
12345678910111213*> 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
Follow these best practices:
Avoid these common mistakes:
1234567891011*> 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
123456789*> 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
12345678910*> 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
Think of record management like managing a filing cabinet:
So record management is like organizing and managing files in a filing cabinet - you open it, work with files, and close it when done!
Complete these exercises to reinforce your understanding:
Create a program that opens a sequential file, reads all records, and displays each record's key information. Handle end of file appropriately.
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.
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.
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.
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.
1. What statement is used to update an existing record?
2. What must you do before reading a record with random access?
3. What file status code indicates end of file?
4. What file status code indicates record not found?
5. What is the correct sequence for updating a record?
6. Which file organization does NOT support DELETE?