The NAME XFD directive assigns a database field name to the field defined on the next line.
This XFD directive has several uses, as shown in the following examples.
Example 1 — Non-unique field names
Within a database file, all field names must be unique. (Multiple database tables may include the same field name, but duplicates may not exist within a single table.) Unique field names are not required in COBOL, because names can be qualified by group items.
For example, the following is acceptable in COBOL:
01 employee-record. 03 date-hired. 05 yy pic 99. 05 mm pic 99. 05 dd pic 99. 03 date-last-paid. 05 yy pic 99. 05 mm pic 99. 05 dd pic 99.
You need not change the field names in your COBOL program to access a database. Instead, you use the NAME XFD directive to provide unique database names for the fields. For example:
01 employee-record. 03 date-hired. 05 yy pic 99. 05 mm pic 99. 05 dd pic 99. 03 date-last-paid. $xfd name=year-paid 05 yy pic 99. $xfd name=month-paid 05 mm pic 99. $xfd name=day-paid 05 dd pic 99.
The dates portion of the resulting database table is as follows.
Example 2 — Names not unique within first 18 characters or longer than 18 characters
Some SQL-based databases require that names be unique within 18 characters, and some require that names be no longer than 18 characters. For those systems, the run time system truncates longer COBOL names after the first 18 characters.
For names that are identical within the first 18 characters, or are not meaningful if shortened to the first 18 characters, use the NAME XFD directive to assign them different database field names.
Suppose you had:
01 acme-employee-record. 03 acme-employee-record-date-hired pic x(6). 03 acme-employee-record-date-last-paid pic x(6).
You could add two NAME XFD directives to differentiate the two item names by making them meaningful within 18 characters:
01 acme-employee-record. $xfd name=date-hired 03 acme-employee-record-date-hired pic x(6). $xfd name=date-last-paid 03 acme-employee-record-date-last-paid pic x(6).
Note that your COBOL names have not changed. The new names are used only for the database fields.
Each time you compile your program to create XFDs, any field names longer than 18 characters is checked for uniqueness within the first 18. If any field names are identical for the first 18 characters, a compiler warning message appears. A warning of this type does not prevent the program from compiling, and the XFD is generated.
Example 3 — Assigning shorter names
You may want to use the NAME XFD directive to assign shorter names than those used in your COBOL programs. This makes the formation of interactive SQL queries easier and quicker. For example:
$XFD NAME=EMPNO 03 employee-number pic x(8).
This XFD directive causes the XFD to map EMPLOYEE-NUMBER to EMPNO in the database.
Example 4 — Matching field names and COBOL FDs
If your database already exists, and a field name in the database does not match the name used in your COBOL FD, you can use a NAME XFD directive to associate the two names. For example:
$xfd name=employee-no 03 employee-number pic x(8).
This XFD directive causes the XFD to map EMPLOYEE-NUMBER in the COBOL program to EMPLOYEE-NO in the database.
Example 5 — Renaming fields that begin with a numeric character
If your COBOL program uses field names that begin with a numeric character, use the NAME XFD directive to assign a different name for use with your database. SQL typically generates a syntax error when it encounters a column name that begins with a numeric character. For example:
03 12-months-sales pic 9(5)V99.
could be renamed this way:
$xfd name=twelve-months-sales 03 12-months-sales pic 9(5)V99.