Referring to a Partial Key

To specify a partial key, you can use a KLIST with fewer KFLD specifications. In free-form calculations, you can also use %KDS with a second parameter indicating the number of keys, or a list of expressions with as many keys as you want. For example, if the file has three keys, but you only want to specify two keys, you can specify the partial key in any of the following ways.
DName+++++++++++ETDsFrom+++To/L+++IDc.Keywords+++++++++++++++++++++++++ 
D keys            DS                  LIKEREC(rec : *KEY)
CL0N01Factor1+++++++Opcode&ExtFactor2+++++++Result++++++++Len++D+HiLoEq
C     klist2        KLIST
C                   KFLD                    k1
C                   KFLD                    k2
 /free
        CHAIN klist2 rec;                // KLIST with two KFLD entries
        CHAIN %KDS(keys : 2) rec;        // %KDS with two keys
        CHAIN (name : %char(id_no)) rec; // a list of two expressions
The rules for the specification of a search argument that refers to a partial key are as follows:
  • The search argument is composed of fields that correspond to the leftmost (high-order) fields of the key for the file or record.
  • Only the rightmost fields can be omitted from the list of keys for a search argument that refers to a partial key. For example, if the total key for a file or record is composed of key fields A, B, and C, the valid search arguments that refer to a partial key are field A, and fields A and B.
  • Each field in the search argument must be identical in attributes to the corresponding key field in the file or record. For search arguments in a list or %KDS used in an I/O operation in free-form calculations, the search argument only needs to match in type. Length and format may be different than the key defined in the file. The attributes include the length, data type, the number of decimal positions, and format (for example, packed or zoned).
  • A search argument cannot refer to a portion of a key field.

If a search argument refers to a partial key, the file is positioned at the first record that satisfies the search argument or the record retrieved is the first record that satisfies the search argument. For example, the SETGT and SETLL operations position the file at the first record on the access path that satisfies the operation and the search argument. The CHAIN operation retrieves the first record on the access path that satisfies the search argument. The DELETE operation deletes the first record on the access path that satisfies the search argument. The READE operation retrieves the next record if the portion of the key of that record (or the record of the specified type) on the access path matches the search argument. The READPE operation retrieves the prior record if the portion of the key of that record (or the record of the specified type) on the access path matches the search argument. For more information on the above operation codes, see the IBM® Rational® Development Studio for i: ILE RPG Reference.