NAME directive

The NAME directive assigns a database field name to the data item defined on the next line. You can use the NAME directive to prevent duplicate field names.

Syntax

$XFD NAME=fieldname

or

*(( XFD NAME=fieldname ))

Examples

This directive has several uses, as shown in the following examples.

Example 1 — Duplicate Names for Data Items in a Single Record

Within a table, 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, this is acceptable in COBOL:

               10  last_visit.
                    15  mm                  pic 99.
                    15  dd                  pic 99.
                    15  yy                  pic 99.
               10  last_payment.  
                    15  mm                  pic 99.
                    15  dd                  pic 99.
                    15  yy                  pic 99.

It is not, however, acceptable for a database table to have two columns with the same name. In fact, you will not be able to compile and generate an XFD with the above source code in your program.

You need not change the field names in your COBOL program to make them accessible as a database column. A preferable option might be to use the NAME directive to provide unique names for the fields. The NAME directive has the advantage of being placed in your COBOL program as a comment, which allows you to change the database name of the column without impacting the existing code or functionality of your COBOL application.

Although this is not included in the sample code, here is an example of using the NAME directive when you have duplicate field names:

               10  last_visit.
                    15  mm                  pic 99.
                    15  dd                  pic 99.
                    15  yy                  pic 99.
               10  last_payment.  
               $XFD NAME=MONTH_PD          
                    15  mm                  pic 99.
               $XFD NAME=DAY_PD
                    15  dd                  pic 99.
               $XFD NAME=YEAR_PD
                    15  yy                  pic 99.

The "dates" portion of the indexed file database table will look like this:

 mm   dd   yy   MONTH_PD   DAY_PD   YEAR_PD 
06 18 02 07 04 02

Example 2 — Assigning Shorter Names

You may want to use the NAME 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:

       FILE SECTION.
       FD  jr-file.
      $XFD COMMENT  This sample file demonstrates directives.
       01  jr-record.
           03  animal-info.
      $XFD NAME=PATIENT 
               05  patient-id                  pic x(5).
               05  atype                       pic x.
               05  ctype redefines atype       pic x.
               05  dtype redefines atype       pic x.
               05  otype redefines atype       pic x.
           03  owner-info.
               05  phone                       pic x(8).
               05  owner                       pic x(30). 
           03  financial.
               05  acct_no.
                   10  year                    pic x(2).
                   10  seq_no                  pic x(4).
      $XFD DATE=mmddyyyy, USE GROUP
               05  last_visit.
                   10  mm                      pic 9(2).
                   10  dd                      pic 9(2).
                   10  yyyy                    pic 9(4).
      $XFD HIDDEN
               05  fee                         pic s9(5)v99.

This directive causes the XFD to map PATIENT-ID to PATIENT in the database.

Additional Examples

Here are some other cases in which you may consider using the NAME directive. In all of these instances, note that your COBOL data does not change. The new name appears only in the indexed file database and in any tables that result from your COBOL code.

  • If your COBOL data contains field names that are identical within the first 18 characters

    Each time you compile your program and specify the CREATEXFD directive, any field names longer than 18 characters are checked for uniqueness within the first 18. If you have names that are identical within the first 18 characters, or that would not be meaningful if shortened to the first 18 characters, use the NAME directive to assign them different database field names.

  • If you map COBOL data items to Windows or Java applications

    If a column name in your Windows or Java application does not match the name used in your COBOL FD, you can use a NAME directive to associate the two names.

  • If a field name in your COBOL data begins with a numeric character

    Because Windows and Java applications communicate using SQL, the program typically generates a syntax error when it encounters a column name that begins with a numeric character. If your COBOL program uses field names that begin with a numeric character, use the NAME directive to assign a different name for use with your Windows and Java applications.

  • If you want to include a "FILLER" item
  • If your COBOL name is an SQL reserved word