RPGPGM.COM - From AS400 to IBM i › Renaming one field in a file in RPG
I was asked to quickly modify a program. The program currently uses a file, I'll call it FILE1, and the modification was to add a second file, FILE2 to it. If the user selects to search for a certain type of information the the program would uses FILE1, if they want to search for a different type of information the program would need to use FILE2.
All of the fields in FILE1 and FILE2 have the same names and attributes, except for the Part number field. The Part number field, PART, in FILE1 is 20 long, in FILE2 it is 15.
I cannot change FILE2 to make PART be the same length as it is in the other file. I must make the minimum amount of changes to the program to negate the need for it to be audited by the SOX auditors. What can I do?
The changes I would need to make would be:
- Create a 'which file' field to be used to determine which file the search is for.
- Add USROPN to the File specifications, F-spec, for FILE1 and FILE2. This means that the file will only be opened if I use the OPEN operation code to open it.
- Use the value in the 'which file' field to condition which file is opened.
- Decide which file to read by using the 'which file' field.
- Close the file opened.
But that still leaves the problem of what to do with the two PART fields.
But how do I cope with the difference between the two PART fields with the minimum of code changes?
Using the PREFIX keyword in the F-spec would not do, as that would rename all the fields in FILE2.
Then it struck me, I could use a way I had used before in RPG III.
Years ago I worked with a company with files that had been developed by Cobol programmers. Many of the field names were longer than six characters, which is the maximum length a field name can be in RPG III. Therefore, they had to be renamed in the RPG programs before they could be used.
What I did was to rename the "external description" of the fields in the Input specification, I-spec. I can do the same here.
Line 1 has the record format name.
The first field name on line 2 is the file name from the file, the second field name is what it will be called in this program.
Now when FILE2 is read, line 3, all I have to do is move the value in PART_2 to PART, line 5, and I don't have to make any more changes.
Do you know of a better way of doing this? If so post it in the Comments below.
To learn more about renaming fields in the I-spec see here.
This article was written for OS i 7.1, and it should work with earlier releases too.
Read the original at RPGPGM.COM - From AS400 to IBM i.