Field procedures
A field procedure is a user-written exit routine that is used to transform values in a single, short string column. You can assign field procedures to a table by specifying the FIELDPROC clause of the CREATE TABLE or ALTER TABLE statement.
When values in the column are changed, or new values
inserted, the field procedure is invoked for each value, and can transform
that value (encode it) in any way. The encoded value is then stored.
When values are retrieved from the column, the field procedure is
invoked for each value, which is encoded, and must decode it back
to the original string value.
Any indexes, including partitioned indexes, defined on a column
that uses a field procedure are built with encoded values. For a partitioned
index, the encoded value of the limit key is put into the LIMITKEY
column of the SYSINDEXPART table. Hence, a field procedure might be
used to alter the sorting sequence of values entered in a column.
For example, telephone directories sometimes require that names like McCabe
and MacCabe
appear
next to each other, an effect that the standard EBCDIC sorting sequence
does not provide. And languages that do not use the Roman alphabet
have similar requirements. However, if a column is provided with a
suitable field procedure, it can be correctly ordered by ORDER BY.
The transformation your field procedure performs on a value is called field-encoding. The same routine is used to undo the transformation when values are retrieved; that operation is called field-decoding. Values in columns with a field procedure are described to Db2 in two ways:
- The description of the column as defined in CREATE TABLE or ALTER TABLE appears in the catalog table SYSIBM.SYSCOLUMNS. That is the description of the field-decoded value, and is called the column description.
- The description of the encoded value, as it is stored in the data base, appears in the catalog table SYSIBM.SYSFIELDS. That is the description of the field-encoded value, and is called the field description.
Important: The field-decoding function must be the exact
inverse of the field-encoding function. For example, if a routine
encodes 'ALABAMA' to '01', it must decode '01' to 'ALABAMA'. A violation
of this rule can lead to an abend of the Db2 connecting
thread, or other undesirable effects.