When I need to load a variable number of records into a table, I generally use one of three approaches:
Guess a maximum size for the table ("MAX-TABLE-SIZE"). Make the table OCCUR 1 TO MAX-TABLE-SIZE TIMES DEPENDING ON INPUT-FILE-RECORD-COUNT. Count the number of input records while loading them to the table. If the number of records in the input file is greater than MAX-TABLE-SIZE, display the table size needed and STOP RUN. Then manually recompile the program with a higher value for MAX-TABLE-SIZE and rerun the program.
Use two programs. The first one counts the number of records in the input file, writes a second source code file with the necessary MAX-TABLE-SIZE, compiles the second program, and executes the second program. This approach avoids manually rerunning the program, but takes longer because a compilation is always needed.
Write data to a file, rather than to a table. Then the data can have as many records as available disk space allows. If no longer needed, delete the file at the end of the program. This approach avoids needing to recompile the program for larger data, but reading from and writing to disk is slower than using an in-memory table.
Are there other techniques in COBOL for handling a variable number of records? What are their advantages/disadvantages?