- Often COBOL programs manage information about employees or customers by storing their data in large sequential files. Then daily or weekly, transaction files are created which contain the changes required to these records. For example, the transaction pile might contain additions, changes, or even deletions of the main records in the master file. Let's continue to work with our employee program. This time we will need a total of four files. The first file is the master file that contains a list of all the current employees.
As you can see on line seven, I have SELECT EMPLOYEEFILE. That's our master file. Next, I have a transaction file, which in this case only contains new employees. When I read the transaction file and compare it to the employee file, I'm actually going to merge the two and create a third file. The new employee file. And finally, the last file is the output file, which is assigned the name ErrorReport.lpt, which will contain a list of employees that are duplicates.
In other words, the program has a transaction file that has the same employee ID as an employee ID already in our master record. I want to walk through the code to see what changes have to be made, but first, let's look at the employee transaction file. As you can see, here is our original employee data file. We have eight employees, and notice that their employee numbers are in sorted order. If I switch over to the transaction file, you can see I'm going to add six new employees.
And again, their employee IDs are in sorted order. The employee IDs will be interspersed with the current records, so that I maintain my order by employee ID. Okay, now that we've seen the transaction file and the employee file, let's go back over to the code. I'm going to scroll down to our data division. Here we can see I have four different file descriptions. The employee file, the transaction file, called TRANSFILE, the new employee file, and the output file.
You might have noticed that even the employee file's a little different. The only variable and the only piece of data that I'm concerned with is the employee ID, so I have the rest of the file declared as filler. The transaction file is the same way. I have the new employee ID, and I have the other 38 characters defined as filler. Finally, the new employee record is going to be a total of 45 characters, and the print line is 132, since most print reports are 132 characters across.
You might have also noticed that both of the two input files contain the end of file variable defined as an 88 level to test for end of file. So when you have more than one, make sure you give each one a unique name. So on line 26, I have ENDOFFILE for my employee file, and on line 32 I have ENDOFFILE2 for my transaction file. Okay, let's continue down to the procedure division. In the procedure division, we don't want to forget to open our files first.
So we have open statements for each of the four files. Two files are being opened as input, and two as output. For the input files it's always a good idea to check the error code to make sure the file is found. As you can see for both files, I have IF FILE-CHECK-KEY is NOT = "00", then display an error message and end the program. On line 90, I read the first record from the employee file and on 94 I read the first record for the transaction file. Once both records are read, you'll notice on line 98 I perform PROCESS-EMPLOYEES UNTIL ENDOFFILE, which is the end of my employee file, and ENDOFFILE2, which is the end of my transaction file.
I want to make sure that I read both files all the way through so that I can merge all the records. Let's scroll down to 200, PROCESSEMPLOYEES. In the PROCESSEMPLOYEES paragraph, we start with an evaluate statement that says EVALUATE TRUE, which continues to execute the statements in this paragraph until both files have reached ENDOFFILE. Now let's add our code to check to see if this is a new employee. So starting on line 107, I'm going to add a WHEN statement.
WHEN employee ID is less than the new employee ID, so when the ID on my original file is smaller than the ID on my transaction file, then I know that I haven't gotten to a new employee yet, and I want to write the current employee out to my master file. So I will write NEWEMPRECORD FROM EMPDETAILS. Then, I will read a new record for my EMPLOYEEFILE.
And AT END, I want to make sure I set my ENDOFFILE to TRUE. And I want to END-READ. Okay, that's what should happen when the employee ID of the employee file, the master file, is less than the records on the transaction file. Now we have to take care of the next scenario. What happens when the employee ID is greater than the new employee ID, which means that I have to insert my new employee into the file? So if that's the case, I'm going to WRITE, and this time I'm going to write the NEWEMPRECORD FROM my employee transaction file.
Now, I need to read the next record in the transaction file, so READ TRANSFILE, and again, AT END SET, don't forget, this is the second one, so ENDOFFILE2 to TRUE. That's the end of my read statement, so I want to do an END-READ. And we're almost done. The last thing I want to check is to make sure it's not a duplicate. So this time I'll say WHEN the employee ID is equal to the new employee ID, and in that case, I want to MOVE the employee ID to my detailed report item, so DET-EMP-ID, and now I can write that record to my error report.
And next I can READ my next record for my TRANSFILE. And AT END SET ENDOFFILE2 to TRUE. Because the TRANSFILE had a duplicate, I want to go to the next record and see if there's any more that I need to add. And finally, I'll end the read. Okay, the last statement in this paragraph is the END-EVALUATE. I'm going to go ahead and save my program. At this point, once we perform the 0200 paragraph until ENDOFFILE for both files, if we go back up to our main paragraph, you can see the next thing is to perform 9000 END-PROGRAM.
So if I scroll down, you'll see that's where I'm closing the employee file, the trans file, the new employee file, and the output file, and I'm executing the STOP RUN. This type of logic is common in many existing mainframe COBOL applications. The new file that I created is then renamed as input for the next day, and the next set of new employees. Now that we've written our program, let's run it and make sure it works. So here's our input data, our employee data. We have eight employees, and I'm going to merge it with these employees.
There's six, but if you look at the very last record, it has the same ID. The employee ID is 800 for Mr. Potato Chips. And the employee ID over here in my original file for 800 is Cheri Slattery. So I should end up with 13 records in my new master file, plus one record written to my error report. Let's check it out. Of course, the first thing I want to do is compile my program. It looks like it compiled successfully, so now I can run it. Okay, it's not very exciting when I compile and run it, but let's look at the files and see what happened.
I'm going to open my new employee file. Which is EMPFILENEW. As you can see, I have 13 records. And you'll notice that Cheri Slattery is still there. Now, let's open up our error report. For this run, the error report has one employee ID that was duplicate. The 000800. So this report would be sent back to the user so they can try and figure out whether the existing employee or the new employee has the wrong employee ID.
This course is designed to help new and experienced programmers alike add COBOL (or add COBOL back) to their skill set. Peggy Fisher shows how to get a COBOL development environment up and running and how to start programming. She reviews COBOL's data types and constants, control structures, file storage and processing methods, tables, and strings. Challenges issued along the way will help you practice what you've learned.
- Downloading and installing Cygwin and GNU COBOL
- Editing, compiling, linking, and running COBOL programs
- Describing data in COBOL
- Working with verbs and expressions
- Using branching
- Reading and writing sequential files
- Updating and deleting records
- Working with relative and indexed files
- Creating and searching tables
- Handling strings