COBOL Table Declaration: How to Use OCCURS for Data Arrays ????

COBOL Table Declaration: How to Use OCCURS for Data Arrays ????


1?? What is OCCURS in COBOL?

The OCCURS clause defines a repeating group of fields within a structure, allowing you to store multiple values under a single table name. Instead of creating separate variables (ITEM-1, ITEM-2, ITEM-3...), you define a single item with OCCURS.

?? Basic Syntax:

01  DATA-TABLE.
    05 ITEM  PIC X(10) OCCURS 5 TIMES.        

Here, ITEM is an array of 5 elements, each holding 10 characters.


2?? Example: Storing 5 Customer Names in an Array

IDENTIFICATION DIVISION.
PROGRAM-ID. CUSTOMER-TABLE.
DATA DIVISION.
WORKING-STORAGE SECTION.
01  CUSTOMER-NAMES.
    05 CUSTOMER  PIC X(20) OCCURS 5 TIMES.

PROCEDURE DIVISION.
    MOVE 'John Doe' TO CUSTOMER(1).
    MOVE 'Alice Smith' TO CUSTOMER(2).
    MOVE 'Bob Johnson' TO CUSTOMER(3).
    
    DISPLAY "First Customer: " CUSTOMER(1).
    DISPLAY "Second Customer: " CUSTOMER(2).
    
    STOP RUN.        

?? Output:

First Customer: John Doe  
Second Customer: Alice Smith        

? Pros: Cleaner and more maintainable than declaring separate variables.


3?? Accessing OCCURS with PERFORM (Loops)

To process all elements in a table, use a PERFORM loop with an index:

WORKING-STORAGE SECTION.
       01  CUSTOMER-TABLE.
           05  CUSTOMER  PIC X(20) OCCURS 5 TIMES INDEXED BY CUSTOMER-IDX.

       PROCEDURE DIVISION.
           PERFORM VARYING CUSTOMER-IDX FROM 1 BY 1 UNTIL CUSTOMER-IDX > 5
               DISPLAY "Customer " CUSTOMER-IDX ": " CUSTOMER(CUSTOMER-IDX)
           END-PERFORM.        

? Best Use Case: When you need to iterate over an unknown number of records.


4?? OCCURS with DEPENDING ON – Dynamic Arrays

Want to define a flexible table size at runtime? Use OCCURS DEPENDING ON:

01  MAX-CUSTOMERS  PIC 9(2) VALUE 3.
01  CUSTOMER-NAMES.
    05 CUSTOMER  PIC X(20) OCCURS 1 TO 10 TIMES DEPENDING ON MAX-CUSTOMERS.        

?? Here, the array size can be changed dynamically based on MAX-CUSTOMERS.


5?? OCCURS with MULTIPLE LEVELS – 2D Tables

You can nest OCCURS inside OCCURS to create multi-dimensional tables.

?? Example: Storing 5 customers, each with 3 transaction amounts

01  CUSTOMER-DATA.
    05 CUSTOMER OCCURS 5 TIMES.  *> 5 Customers (1st dimension)
        10 NAME        PIC X(20).
        10 TRANSACTIONS PIC 9(6)V99 OCCURS 3 TIMES.  *> 3 Transactions per Customer (2nd dimension)
        

To access a specific transaction of a customer:

       MOVE "John Doe" TO NAME(1).  *> Customer 1
       MOVE 1250.75 TO TRANSACTIONS(1,1).  *> First transaction of first customer
       MOVE 890.50 TO TRANSACTIONS(1,2).  *> Second transaction of first customer

       MOVE "Alice Smith" TO NAME(2).  *> Customer 2
       MOVE 3000.00 TO TRANSACTIONS(2,1).  *> First transaction of second customer        

? Best Use Case: When you need to store structured data like customer orders, sales, or matrix-based calculations.


6?? OCCURS vs. Indexed Files – When to Use?


?? Rule of Thumb:

  • Use OCCURS when working within program memory.
  • Use indexed files for large datasets requiring search operations.


7?? Key Takeaways

?? OCCURS simplifies handling multiple data items in a structured way.

?? PERFORM VARYING helps iterate over table elements efficiently.

?? OCCURS DEPENDING ON allows dynamic sizing.

?? Nested OCCURS enables multi-dimensional data storage.

Fabricio Dorneles

Senior Front-end Developer | React - NextJS - Typescript - NodeJS - AWS

1 天前

Great Content! Thanks!

Peter Sterwe

Cobolskolan Sverige | Professionell utbildning f?r blivande COBOL-proffs | w ww.cobolskolan.se

1 天前

Some notes: No 3: It is not an index, it is a subscript. Index is different. No 4: The size never changes (always max 10). Depending on tells COBOL how many elements COBOL uses when using COBOL Search. No 5: The table is not declared as a 2D correctly. By the way, its called a TABLE in COBOL, not Array ??

Julio César

Senior Software Engineer | Java | Spring Boot | React | Angular | AWS | APIs

1 天前

Very Nice Post!

Kaique Perez

Fullstack Software Engineer | Node | Typescript | React | Next.js | AWS | Tailwind | NestJS | TDD | Docker

2 天前

Good to know. Thanks for sharing!

Wesley M.

Mainframe Developer | COBOL CICS DB2 VSAM | NATURAL ADABAS | DevOps | Card Processing | Banking Systems

2 天前

Great explanation. About OCCURS DEPENDING ON, it's a great resource for improve performance in online transactions.

要查看或添加评论,请登录

Luiz Melo的更多文章

  • INSPECT vs. UNSTRING in COBOL: Mastering String Manipulation ????

    INSPECT vs. UNSTRING in COBOL: Mastering String Manipulation ????

    1?? INSPECT – The Quick Way to Analyze and Modify Strings The statement is used to count, replace, or convert…

    12 条评论
  • Understanding SQL Commands: DDL, DML, DCL, TCL, and DQL

    Understanding SQL Commands: DDL, DML, DCL, TCL, and DQL

    1. Data Definition Language (DDL) DDL commands define and manage the structure of a database, including tables…

    22 条评论
  • Common ABEND Codes in COBOL

    Common ABEND Codes in COBOL

    1. S0C4 – Protection Exception (Storage Violation) Cause: Attempt to access restricted or unallocated memory…

    46 条评论
  • Using the ADABAS Database in the NATURAL Language

    Using the ADABAS Database in the NATURAL Language

    ADABAS Structure ADABAS uses a data model based on records organized into files, where each file contains multiple…

    25 条评论
  • Exploring CICS: The IBM Transaction Monitor

    Exploring CICS: The IBM Transaction Monitor

    Key Features CICS offers several functionalities for transactional applications, including: Transaction Management:…

    39 条评论
  • ADABAS vs. DB2: Comparing Databases in Mainframe Environments

    ADABAS vs. DB2: Comparing Databases in Mainframe Environments

    What is ADABAS? ADABAS (Adaptable Database System) is a database management system (DBMS) developed by Software AG. It…

    42 条评论
  • The Importance of ROSCOE in the COBOL Environment

    The Importance of ROSCOE in the COBOL Environment

    What is ROSCOE? ROSCOE is an interactive work environment designed for mainframe systems. It provides a powerful…

    36 条评论
  • The Importance of TSO in the COBOL Environment

    The Importance of TSO in the COBOL Environment

    What is TSO? TSO is an interactive interface that allows users to access the z/OS operating system on a mainframe. It…

    77 条评论
  • COBOL Modernization

    COBOL Modernization

    Evolving Legacy Systems COBOL modernization refers to initiatives aimed at updating legacy applications written in this…

    24 条评论
  • The Importance of JCL in Mainframe

    The Importance of JCL in Mainframe

    Job Control Language (JCL) is an essential tool in mainframe environments, serving as the interface between users and…

    33 条评论