The ACCESS clause represents one of COBOL's most fundamental and sophisticated file management mechanisms, serving as the primary determinant for how data files are accessed, organized, and manipulated throughout the lifecycle of business applications. Far more than a simple file access directive, the ACCESS clause embodies COBOL's comprehensive approach to data management by providing precise control over file access patterns, data retrieval strategies, performance optimization techniques, and complex file organization methods that enable applications to handle massive datasets while maintaining the integrity and performance characteristics that make COBOL ideal for enterprise data processing environments requiring sophisticated file management capabilities.
1234SELECT CUSTOMER-FILE ASSIGN TO "CUSTOMERS.DAT" ORGANIZATION IS SEQUENTIAL ACCESS MODE IS SEQUENTIAL FILE STATUS IS WS-FILE-STATUS.
Sequential access reads records in order, optimized for batch processing.
123456SELECT CUSTOMER-FILE ASSIGN TO "CUST.IDX" ORGANIZATION IS INDEXED ACCESS MODE IS RANDOM RECORD KEY IS CUST-ID ALTERNATE RECORD KEY IS CUST-NAME WITH DUPLICATES FILE STATUS IS WS-IDX-STATUS.
Random access enables direct record retrieval using keys.
123456SELECT INDEXED-FILE ASSIGN TO "DYNAMIC.IDX" ORGANIZATION IS INDEXED ACCESS MODE IS DYNAMIC RECORD KEY IS PRIMARY-KEY ALTERNATE RECORD KEY IS SEARCH-KEY WITH DUPLICATES FILE STATUS IS WS-DYN-STATUS.
Dynamic access combines both sequential and random access patterns.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152IDENTIFICATION DIVISION. PROGRAM-ID. ACCESS-DEMO. ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT CUSTOMER-FILE ASSIGN TO "CUSTOMER.IDX" ORGANIZATION IS INDEXED ACCESS MODE IS DYNAMIC RECORD KEY IS CUST-ID ALTERNATE RECORD KEY IS CUST-NAME WITH DUPLICATES FILE STATUS IS WS-FILE-STATUS. DATA DIVISION. FILE SECTION. FD CUSTOMER-FILE. 01 CUSTOMER-RECORD. 05 CUST-ID PIC X(10). 05 CUST-NAME PIC X(30). 05 CUST-BALANCE PIC S9(8)V99. 05 CUST-STATUS PIC X(10). WORKING-STORAGE SECTION. 01 WS-FILE-STATUS PIC X(2). 88 FILE-SUCCESS VALUE "00". 88 FILE-EOF VALUE "10". 88 RECORD-NOT-FOUND VALUE "23". PROCEDURE DIVISION. MAIN-PROCESSING. OPEN I-O CUSTOMER-FILE *> Random access example MOVE "CUST00001" TO CUST-ID READ CUSTOMER-FILE KEY IS CUST-ID IF FILE-SUCCESS DISPLAY "Customer found: " CUST-NAME ELSE IF RECORD-NOT-FOUND DISPLAY "Customer not found" END-IF *> Sequential access example START CUSTOMER-FILE KEY IS >= CUST-ID PERFORM UNTIL FILE-EOF READ CUSTOMER-FILE NEXT RECORD IF FILE-SUCCESS DISPLAY "Processing: " CUST-NAME END-IF END-PERFORM CLOSE CUSTOMER-FILE STOP RUN.