Sign in

docs RPG Reference

ALIAS

ALIAS

When the ALIAS keyword is specified for an externally-described data structure, the RPG compiler uses the alias (alternate) names for the subfields, if present. If the ALIAS keyword is not specified for the data structure, or an external field does not have an alias name, the RPG compiler uses the standard external field name.

Note: If the alias name for a particular external field is enclosed in quotation marks, the standard external field name is used for that field.

When alias names are being used and you want to rename a subfield, you specify the alias name as the parameter to the EXTFLD keyword. The EXTFLD keyword does not support continuation, so you must specify the entire name on one source specification. Figure 1 shows an example with two data structures, which are defined for the same file. The alias name, CUSTOMER_ADDRESS, is specified as the parameter for the EXTFLD keyword for the data structure with the ALIAS keyword. The standard name, CUSTAD, is specified as the parameter for the EXTFLD keyword for the data structure without the ALIAS keyword.

When the PREFIX keyword is specified with the ALIAS keyword, the second parameter of PREFIX, indicating the number of characters to be replaced, does not apply to the alias names. In the following discussion, assume that the external file MYFILE has fields XYCUSTNM and XYID_NUM, and the XYCUSTNM field has the alias name CUSTOMER_NAME.

  • If keyword PREFIX(NEW_) is specified, with no second parameter, no characters are replaced for any names. The names that are used for the RPG subfields are NEW_CUSTOMER_NAME and NEW_XYID_NUM.
  • If keyword PREFIX(NEW_:2) is specified, two characters are removed from the names of fields that do not have an alias name. The names that are used for the RPG subfields are NEW_CUSTOMER_NAME and NEW_ID_NUM. The first two characters, “XY”, are replaced in XYID_NUM, but no characters are replaced in CUSTOMER_NAME.
  • If keyword PREFIX(”:2) is specified, two characters are removed the names of fields that do not have an alias name. The names that are used for the RPG subfields are CUSTOMER_NAME and ID_NUM. The first two characters, “XY”, are replaced in XYID_NUM, but no characters are replaced in CUSTOMER_NAME.

Figure 1. Using the ALIAS keyword for an externally-described data structure

 * The DDS specifications for file MYFILE, using the ALIAS keyword
 * for the first two fields, to associate alias name CUSTOMER_NAME
 * with the CUSTNM field and alias name CUSTOMER_ADDRESS
 * with the CUSTAD field.
A          R CUSTREC
A            CUSTNM        25A         ALIAS(CUSTOMER_NAME)
A            CUSTAD        25A         ALIAS(CUSTOMER_ADDRESS)
A            ID_NUM        12P 0

 * The RPG source, using the ALIAS keyword.
 * The customer-address field is renamed to CUST_ADDR
 * for both data structures.
D aliasDs       e ds                  ALIAS
D                                     QUALIFIED EXTNAME(myfile)
D   cust_addr   e                     EXTFLD(CUSTOMER_ADDRESS)
D noAliasDs     e ds
D                                     QUALIFIED EXTNAME(myfile)
D   cust_addr   e                     EXTFLD(CUSTAD)

    // The ALIAS keyword is specified for data structure "aliasDs"
    // so the subfield corresponding to the "CUSTNM" field has
    // the alias name "CUSTOMER_NAME"
    aliasDs.customer_name = 'John Smith';
    aliasDs.cust_addr = '123 Mockingbird Lane';
    aliasDs.id_num = 12345;

    // The ALIAS keyword is not specified for data structure
    // "noAliasDs", so the subfield corresponding to the "CUSTNM"
    // field does not use the alias name
    noAliasDs.custnm = 'John Smith';
    noaliasDs.cust_addr = '123 Mockingbird Lane';
    noAliasDs.id_num = 12345;