RPG IV External Data structure
An external data structure in RPG ILE is based on external physical file object. All fields declared in the physical file become subfields of data structure. Since the declaration of field names and their data types is external to the RPG program, such data structures are called external data structures.
Example of an External data structure:
This is how an external data structure is defined.
DName+++++++++++ETDsFrom+++To/L+++IDc.Keywords++++++++++++++++++
D Name E DS ExtName(ExtPfName)
The letter 'E' tells the compiler to define the subfields of the data structure from the definition of fields of physical file ExtPfName.
Name conflict handling:
If we declare a data structure definition to be external and use the file also, RPG IV compiler will not be able to distinguish between the sub fields and file fields. In that situation we should rename the data structure sub fields.
To rename all fields of an external data structure we can use PREFIX keyword at the DS level. The following example will clarify this
DName+++++++++++ETDsFrom+++To/L+++IDc.Keywords++++++++++++++++++
D Name E DS Extname(ExtName) Prefix(A_)
Now all the subfields of this data structure will begin with A_. That is if the file fields are as below.
Name
Age
The data structure fields will be A_Name and A_Age… like that.
When defining data structure based on external file, we might need to add our own new fields. These fields generally overlap already defined fields’ definitions, however, we may define non overlapping fields such that the total length of the data structure increases.
To add new subfields in an external RPG IV data structure we can follow the following syntax.
D Name E DS Extname(Name1) Prefix(A_)
D subfield1 from to
Example of External Data Structure Program in RPG ILE
The following example encompasses concepts learned so far.
DDS of the physical file EMPDTA on which this example(External data structure in fact) is based upon.
.....A..........T.Name++++++RLen++TDpB......Functions+++++++++
A R EMPDTA01
A FNAME 10 TEXT('FIRST NAME')
A LNAME 10 TEXT('LAST NAME')
Source of the program using external data structure
DName+++++++++++ETDsFrom+++To/L+++IDc.Keywords++++++++++++++++++
D Emp E DS Extname(EmpDta) Prefix(A_)
D FullName 1 20
D Sex 21 21
*
CL0N01Factor1+++++++Opcode&ExtExtended-factor2++++++++++++++++++
C Eval A_FName = 'Angelina'
C Eval A_LName = 'Jolie'
C Eval Sex = 'F'
*
C Emp Dsply
C A_FName Dsply
C A_LName Dsply
C FullName Dsply
C Sex Dsply
Please notice that FullName is used without the prefix. The prefix ‘A_’ does not apply to custom sub fields. Also, we can define custom sub fields to be overlapping as well as extending the data structure.
Output of External data Structure Program
DSPLY Angelina Jolie F
DSPLY Angelina
DSPLY Jolie
DSPLY Angelina Jolie
DSPLY F
External data structure FAQ’s
1. Is the data structure field definition picked from the source of the physical file?
No, the definition is picked from the physical file object.
2. Is the external data structure automatically populated from the file fields?
Apart from the definition, there’s no relationship between the physical file and the RPG data structure.
3. Is it required to keep the physical file even after not creating the program object?
No, it’s your prerogative. Deleting the physical file object does not affect RPG program in any way.
4. Why do we use external data structures?
Defining external data structures helps us easier maintenance of the program. An external data structure based RPG file maintenance program frees you from any further maintenance of itself. If you add/change/delete a field in the physical file, you will just need to compile the file maintenance program! That’s it. Nothing else is required.
If you are using display files, you will have to make a few changes there.