- In addition to inserting new records, it's common to have changes, and even records that need to be deleted from the master file. The process flow for this is that the master file will be read along with the transaction file. Every master record is read and checked against the transaction file for changes. If none exist, it is written to a new master file as output. If changes need to be made, they are applied, and then the updated record is also written to the new master file. For this sample program, the transaction file has a new field added to the end to indicate whether the record is a change or a delete.
If the transaction code is "C" for "change," the master record will be replaced with the transaction record information. If the transaction code is a D, then the record is simply ignored from both files, and the next master record and next transaction record are both read. After all changes and deletions are made, the new master file is then renamed to be the input file for the following day's transactions. Let's take a look at our code. Starting on line seven, we declare our employee file, then we have our trans file, and our new employee file.
Notice, the file description for the trans file which is on line 27, has a new field added called the trans code. As I stated earlier, this example, the trans code will either be a C or a D for "change." Another option would be to use an A for "add" if you wanted to add records in the same program. Okay, I have split screen here in Notepad++. Let's take a look at the transaction file. It's called "EMPCHANGES" for "employee changes." As you can see, the first record has a D at the end.
So, I'm going to delete Jesse Jones, I'm going to change the next record, the employee ID 130, I'm going to change the employee ID 325, I'm going to delete employee ID 431, I'm going to change employee ID 599, and employee ID 800. Okay, let's go back to the code, and take a look at how this is going to happen. One additional element that needed to be added to this program is in working storage. I needed to add a detail line that had the employee ID, and then I just have a variable called "D-E-T," details, on line 61, that just contains the remaining 38 characters without the D or C at the end.
I need that, so that if it's a change, I can use that information to write my new record. Okay, let's scroll down. We open our files, and then on line 90, we perform PROCESS EMPLOYEES until both files have been read. Let me scroll down a little more, and you can see under PROCESS EMPLOYEES, when the employee ID is less than the employee ID on the transaction file, then I know no changes are necessary, and I write that record out to my output file. Next, on line 105, I check to see if the employee ID is greater than the new employee ID.
In that case, I write the new employee record out. Finally, let me scroll down a little bit more, and you'll see, I have two more WHEN statements that check if the employee IDs are equal. The first one, on line 112, checks to see if the transaction code is D for "delete." If it is, I'm going to skip both the record incoming from the master file, and the record on the transaction file. So, if that's true, I just say, "Read trans file" to get the next transaction record, and, "Read employee file." On line 120, that is where I check to see if the employee ID is the same, and the transaction code is C for "change." When the employee ID is equal to the new employee ID, and the trans code is equal to C for "change," then, I need to move the information from the transaction file to a working storage space, the detail, EMPID, and detail details.
Then, I write the new employee record from the detail line. And now, I need to read the next transaction record and the next employee record, since the ones I just read were equal, and I took care of the changes. At the bottom of the program, I close my files, and I have a STOP RUN. Okay, let's see what happened when I ran the program. Remember, this was my master file. It had 13 records, this is my transaction file, which had six records, but two of them have D for "delete." So, my new file should only have 11 records.
So, if I go to my EMPFILE NEW, you'll see there's 11 records, and you can see the changes to the 130 record, and it says "Darth Maul" instead of "Darth Vader," and the last one says "potato chips" instead of "cherry slattery." So, it looks like my program worked.
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