Demonstrates how to insert records into a sequential file from a file of transaction records. A new file is created which contains the inserted records.

Sample data: STUDENTS.dat (first 5 of 32 records)
8712351SMITH   MS19671012LM51F
8712352POWER   TG19681219LM51M
8712353SWEENEY ST19690905LM52M
8712354WALSH   SM19700313LM60M
8712355WILLIAMSTJ19650128LM51M
Sample data: TRANSINS.dat (5 records)
8700234Insert1 ok19531223LM60M
8805733Insert2 ok19631009LM51F
8912345Insert3 ok19570723LM53F
9012354Rec Exists19520418LM60F
9123453Insert4 ok19591230LM53F
       >>SOURCE FORMAT IS FREE
IDENTIFICATION DIVISION.
PROGRAM-ID. InsertRecords.
AUTHOR. Michael Coughlan.
*> This program updates the Students.dat file with insertions
*> taken from the Transins.dat file to create a new file
*> - Students.New - which contains the inserted records.

ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
      SELECT StudentRecords ASSIGN "STUDENTS.dat"
             ORGANIZATION IS LINE SEQUENTIAL
             ACCESS MODE IS SEQUENTIAL.

      SELECT TransRecords ASSIGN "TRANSINS.dat"
             ORGANIZATION IS LINE SEQUENTIAL
             ACCESS MODE IS SEQUENTIAL.

      SELECT NewStudentRecords ASSIGN "STUDENTS.new"
             ORGANIZATION IS LINE SEQUENTIAL
             ACCESS MODE IS SEQUENTIAL.


DATA DIVISION.
FILE SECTION.
FD StudentRecords.
01 StudentRecord.
   88 EndOfStudentFile     VALUE HIGH-VALUES.
   02 StudentID            PIC X(7).
   02 FILLER               PIC X(23).

FD TransRecords.
01 TransRecord.
   88 EndOfTransFile       VALUE HIGH-VALUES.
   02 TransStudentID       PIC X(7).
   02 FILLER               PIC X(23).

FD NewStudentRecords.
01 NewStudentRecord        PIC X(30).




PROCEDURE DIVISION.
BEGIN.
    OPEN INPUT StudentRecords
    OPEN INPUT TransRecords
    OPEN OUTPUT NewStudentRecords

    READ StudentRecords
       AT END SET EndOfStudentFile TO TRUE
    END-READ

    READ TransRecords
       AT END SET EndOfTransFile TO TRUE
    END-READ

    PERFORM UNTIL (EndOfStudentFile) AND (EndOfTransFile)
       EVALUATE TRUE
         WHEN (StudentID < TransStudentID)
              WRITE NewStudentRecord FROM StudentRecord
              READ StudentRecords
                 AT END SET EndOfStudentFile TO TRUE
              END-READ

         WHEN (StudentID > TransStudentID)
              WRITE NewStudentRecord FROM TransRecord
              READ TransRecords
                  AT END SET EndOfTransFile TO TRUE
              END-READ

         WHEN (StudentID = TransStudentID)
              DISPLAY "Error - " TransStudentId " already exists in file"
              READ TransRecords
                  AT END SET EndOfTransFile TO TRUE
              END-READ
       END-EVALUATE
    END-PERFORM
    
    CLOSE StudentRecords
    CLOSE TransRecords
    CLOSE NewStudentRecords
    STOP RUN.