Keys

Vision files allow up to 120 keys for sorting. One key is the primary key, all others are alternates. In a record, a key may not occupy physical space that exceeds the minimum record length. Thus, a 10-byte key cannot occupy positions 20-29 in a record with a minimum record length of 28.

You can specify whether duplicates are allowed for the primary key and for each alternate key. If duplicates are allowed for a particular key, it is possible to write a record whose key fields contain exactly the same data as the key fields of an existing record. In this case, the records are stored in chronological order.

Note: It is generally recommended that you not allow duplicate primary keys unless the file is processed only in ACCESS SEQUENTIAL mode. Allowing duplicate primary keys in files that are processed in DYNAMIC and RANDOM access modes, could generate unanticipated results. The rules that govern the treatment of duplicate primary key values are presented in File-Control Paragraph.

Every alternate key causes significant additional overhead. (Keys have the least amount of overhead when duplicates are allowed to occur.) The keys for a data file are automatically stored in a compressed form.

A key may be a contiguous part of the records, or it may be split into as many as 16 segments. If you are compiling for compatibility with versions earlier than Vision Version 4, you can have no more than six segments. You must use Vision Version 4, or later, if you want more than six segments.

Suppose you have the following record structure in a file called AJAX-SUPPLIES:

01  CUSTOMER-RECORD.
    03  CUSTOMER-NO           PIC 9(6).
    03  CUSTOMER-BALANCE      PIC S9(9)V99.
    03  CUSTOMER-NAME         PIC X(30).
    03  CUSTOMER-CONTACT      PIC X(30).

To use CUSTOMER-NAME as the primary key, you would use the syntax shown in the last line below:

FILE-CONTROL.
     SELECT AJAX-SUPPLIES
     ASSIGN TO DISK "INDEX.DAT"
     RECORD KEY IS CUSTOMER-NAME.

If data elements are contiguous and defined in the order that would be used for sorting, they may be grouped and defined together as a key. For example, suppose you wanted to use CUSTOMER-BALANCE, CUSTOMER-NAME as an alternate key. Because these two fields are contiguous and are defined in the same sequence they will be used for sorting, the most efficient way to define the alternate key is to establish a group item that includes both fields. For example:

01  CUSTOMER RECORD
    03  CUSTOMER-NO               PIC 9(6).
    03  CUSTOMER-BALNAME.
        05  CUSTOMER-BALANCE      PIC S9(9)V99.
        05  CUSTOMER-NAME         PIC X(30).
    03  CUSTOMER-CONTACT          PIC X(30).

Then, to define CUSTOMER-BALNAME as an alternate key, you would use the syntax shown in the last line below:

FILE-CONTROL.
     SELECT AJAX-SUPPLIES
     ASSIGN TO DISK "INDEX.DAT"
     RECORD KEY IS CUSTOMER-NAME
     ALTERNATE KEY IS CUSTOMER-BALNAME.

Suppose now that you want to define a sort sequence that uses fields that are not contiguous, or are defined in a different order from the sorting order. In this case, you could either:

Split keys allow you to specify up to 16 segments of data elements as the components of a key. (Note that if you compile for compatibility with versions earlier than Vision Version 4, you can have no more than six segments.) The data segments need not be contiguous and need not be listed in the order they appear within the record. The composite length of a split key cannot exceed 250 bytes, and no key can be defined beyond the minimum record length.

For example, to define an alternate key consisting of CUSTOMER-BALANCE, CUSTOMER-NAME, and CUSTOMER-NO, use the syntax shown in the last two lines below:

FILE-CONTROL.
    SELECT AJAX-SUPPLIES
    ASSIGN TO DISK "INDEX.DAT"
    RECORD KEY IS CUSTOMER-NAME
    ALTERNATE RECORD KEY IS CUSTOMER-BALNAME
    ALTERNATE RECORD KEY IS BAL2-KEY = 
      CUSTOMER-BALANCE, CUSTOMER-NAME, CUSTOMER-NO.

In this example, BAL2-KEY is a user-defined word and is the name you would use in your READ and START statements. Note that BAL2-KEY is not defined in Working-Storage. This is the only definition of the key.