DB2 Server for VSE & VM: Application Programming


Using Field Procedures

Field procedures enable you to alter the sorting sequence of values entered in a single short string column (CHAR, VARCHAR, GRAPHIC, or VARGRAPHIC). For some applications the standard EBCDIC sorting sequence is not appropriate. For example, telephone directories sometimes require that names like "McCabe" and "MacCabe" appear next to each other, and the standard sorting routine would separate them. Another example is a national language character set that does not use the Roman alphabet. For example, Kanji (Japanese) can only be sorted properly using a field procedure.

If you assign a field procedure to a column, it is called whenever values in that column are changed or are inserted, and it transforms (encodes) the original value into one value that sorts properly.

When you retrieve a row from the encoded column, the same field procedure decodes it into the original form. You will never see the encoded string. From a user's point of view, all a field procedure does is change the sorting sequence for a column.

For example, consider a table with a short string column that contains the four divisions in a company: North, South, East, and West. Divisions are usually sorted as follows:

   East
   North
   South
   West

You can, however, write a field procedure that encodes North as 1, South as 2, East as 3 and West as 4. The divisions would then be sorted as follows:

   North
   South
   East
   West

Note:The encoded values do not have to be the same data type as the decoded values. Refer to the DB2 Server for VSE & VM SQL Reference manual for a description of the catalog table SYSCOLUMNS, which contains the descriptions of decoded columns, and SYSFIELDS, which contains the descriptions of the corresponding encoded columns.

While field procedures are used primarily to alter the standard EBCDIC sorting sequence, they can also be used in any application program that requires short strings to be stored differently from how they are inserted or retrieved.

For a sample field procedure and the rules for writing field procedures, refer to the DB2 Server for VM System Administration or the DB2 Server for VSE System Administration manual.

DB2 Server for VSE & VM provides two field procedures for performing cultural sorts. They are:

FP870L2
Sample field procedure for cultural sorting for the Latin 2 code page (Regions: Slovenia, Poland, and Romania).

FP102CY
Sample field procedure for cultural sorting for the Cyrillic code page (Regions: Russia, Bulgaria, Serbia, and Montenegro).

If Data Propagator Capture for VSE or VM is being used on tables that have columns with field procedures, "1-way" field procedures must be defined on the Data Propagator Change Data (CD) tables to properly propagate this data. Refer to the DB2 Server for VM System Administration or the DB2 Server for VSE System Administration manual for more information.

Assigning Field Procedures to Columns

To assign a field procedure to a new column, include the FIELDPROC clause on either the CREATE TABLE or ALTER TABLE statement. To assign field procedures to columns in an existing table, you must unload the data, recreate the table to include the field procedures, and they reload the data back into the table. If you create a column without a field procedure, you cannot add one later.

Refer to the DB2 Server for VSE & VM SQL Reference manual for the syntax diagrams for the CREATE and ALTER TABLE statements. The fieldproc-block for these diagrams is shown below.

Figure 89. fieldproc-block Syntax

>>-FIELDPROC--program_name----+-------------------------+------><
                              |    .-,-----------.      |
                              |    V             |      |
                              '-(-----constant---+---)--'
 

For example:

   ALTER TABLE SCOTT.SUPPLIERS ADD RATING CHAR(6) FIELDPROC MYFLDPRO (10,5)

The constants (10,5), that follow the program_name MYFLDPRO are optional parameters, defined when the field procedure is written and passed to the field procedure when it is invoked.

Understanding Field Procedure Rules

In most cases you will not have to worry about the rules that define when a field procedure encodes or decodes a short string. However, if you understand when the database manager calls field procedures, this can help you understand their performance implications. The less you call field procedures to encode or decode strings the better your application's performance will be.

Understanding when field procedures are called can also help you to avoid some pitfalls. For example, consider a table TABLE_A with a column COLUMN_A that has fieldproc F1, and consider these two statements:

   SELECT SUBSTR(MAX(COLUMN_A,1,5)) FROM TABLE_A
 
   SELECT MAX(SUBSTR(COLUMN_A,1,5)) FROM TABLE_A

You might assume that the two statements should return essentially the same result; however, different results can be returned depending on your coding. In the first statement, the database manager does the following:

  1. Finds the maximum encoded value in COLUMN_A
  2. Decodes the result from MAX with field procedure F1
  3. Applies the SUBSTR function to the decoded value of the result from MAX.

In the second statement, the database manager does the following:

  1. Decodes the value in COLUMN_A with field procedure F1
  2. Applies the SUBSTR function to the decoded value in COLUMN_A
  3. Applies the MAX function to the result of the SUBSTR function.

That is, the first statement MAX is applied to encoded values, and the second is applied to decoded values.

The rest of this section covers the rules that define when a field procedure encodes or decodes a short string.

Input from an Application Program

The field procedure is called to encode data when your application program inserts or updates data. This includes the following statements:

Output to an Application Program

The field procedure is called to decode data when your application program fetches or selects data. This includes the following statements:

Comparison

If a column with a field procedure is compared to a constant, the constant is first encoded by the field procedure. The comparison is then performed between the encoded values in the column and the encoded value of the constant. Host-variables, parameter markers, and the USER special register are treated the same way.

For example, consider the following SQL statement where COLUMN_A has field procedure F1:

   SELECT * FROM MY_TABLE WHERE COLUMN_A > 'SMITH'

When processing the above statement, the database manager first encodes 'SMITH', and then for each row in MY_TABLE, compares F1 to the encoded value in COLUMN_A.

A field procedure can only encode short strings values. If the variable or constant is of a data type other than CHAR, VARCHAR, GRAPHIC or VARGRAPHIC, a negative SQLCODE is returned.

If a column with a field procedure is compared to another column, both columns must have field procedures with the same program_name, comparable encoded data type, and the same CCSID. If not, a negative SQLCODE is returned.

Referential Integrity

If a primary key column has a field procedure, then the foreign key column must have the same field procedure, and the CCSIDs of both key columns must be the same. Otherwise, a negative SQLCODE is returned. For two field procedures to be the same, their program_names, encoded data type, encoded data length, and input parameters must be identical.

For example, the following is correct:

   CREATE TABLE PRIMARY
          (COLUMN_A CHAR(10) FIELDPROC F1 NOT NULL,
           COLUMN_B INTEGER)
          PRIMARY KEY(COLUMN_A)
 
   CREATE TABLE FOREIGN
          (COLUMN_A CHAR(10) FIELDPROC F1 NOT NULL,
           COLUMN_B CHAR(10))
 
   ALTER TABLE FOREIGN
          ADD FOREIGN KEY (COLUMN_A)
          REFERENCES PRIMARY ON DELETE SET NULL

Scalar Functions

All scalar functions operate on decoded values. For example, if 'V' is a string in a column with a field procedure, HEX('V') returns the hexadecimal representation of 'V'. The result is not associated with the original column's field procedure. However, if the result of a scalar function is compared to a column that is associated with a field procedure, this result is encoded by the comparison column's field procedure. The comparison is then made between the encoded value of the column and the encoded result of the scalar function. This is consistent with how columns with field procedures are compared to constants.

For example:

  1. Consider a table (MY_TABLE) with COLUMN_A that has field procedure F1 and COLUMN_B that has field procedure F2. Consider the following SQL statement:
       SELECT * FROM MY_TABLE WHERE COLUMN_A > SUBSTR(COLUMN_B,3,3)
    

    For each row of MY_TABLE, the following occurs:

    1. The encoded values of COLUMN_B are decoded by field procedure F2.
    2. The substring operation is applied to the decoded value of COLUMN_B.
    3. The result of the substring operation is encoded by field procedure F1.
    4. Finally, the encoded value of COLUMN_A is compared to the encoded result of the substring operation.
  2. Consider a table (MY_TABLE) with three columns, where COLUMN_A has field procedure F1, COLUMN_B has field procedure F2, and COLUMN_C is NOT NULL and has field procedure F3. Consider the following SQL statement:
       SELECT * FROM MY_TABLE WHERE COLUMN_A > VALUE(COLUMN_B,COLUMN_C)
    

    For each row of MY_TABLE, the following occurs:

    1. If the value of COLUMN_B is not null, then COLUMN_B is decoded, using F2. Call the result 'M'.
    2. If the value of B is null, then C is decoded, using F3. Call the result 'M'.
    3. 'M' is then encoded using F1.
    4. The encoded result of the VALUE function is then compared to the encoded value of COLUMN_A.

    Note:A field procedure is never called to encode or decode a NULL value. A NULL value always maps to a NULL.

  3. If a column with a field procedure is the argument of the LENGTH function, first it is decoded by the field procedure, and then the length of the result is returned. Of course, if the column data type is a fixed length (for example, CHAR(15)), there is no need to actually decode the column value. The length returned by the function is simply the fixed length of the column (15 in this example).

Column Functions

The column functions MAX and MIN operate on encoded values. The remaining column functions operate on numeric data, and are not affected by field procedures.

Concatenation

The concatenation operator is basically a scalar function, and follows the same rules as a scalar function.

For example, consider a table (MY_TABLE) with COLUMN_A that has field procedure F1 and COLUMN_B that has field procedure F2. Now, consider the following SQL statement:

   SELECT * FROM MY_TABLE WHERE COLUMN_A > 'ADDITION' CONCAT COLUMN_B

For each row in MY_TABLE, the following occurs:

  1. The value of COLUMN_B is decoded by F2.
  2. 'ADDITION' is concatenated with the decoded value in COLUMN_B.
  3. The result of the concatenation is encoded by field procedure F1.
  4. The encoded result of the concatenation is compared to the encoded value of COLUMN_A.

The IN and BETWEEN Predicates

These predicates operate the same as a comparison between a column with a field procedure and a constant.

The LIKE Predicate

This predicate operates on decoded values.

Sorting

Indexes will be based on encoded values. The ORDER BY and GROUP BY clauses will sort the data according to the encoded format. The database manager also sorts values during a UNION operation.

Null Values

While a column with a field procedure may be defined to allow null values, the field procedure is never called to process a null value. A decoded null value always maps to an encoded null value, and an encoded null always maps to a decoded null.

Unions and Joins

The rules for comparing two columns with field procedures apply to unions and joins. The two columns must have the same field procedure.

Sub-SELECTS

All the rules described above apply to sub-SELECTs.

For example:

   SELECT * FROM TABLE_1
            WHERE COLUMN_A=(SELECT COLUMN_B FROM TABLE_2);
 
   SELECT * FROM TABLE_1
            WHERE COLUMN_A IN (SELECT COLUMN_B FROM TABLE_2);

If the columns COLUMN_A and COLUMN_B have different field procedures these statements are invalid (field procedure comparison rules apply). For example:

   INSERT INTO T1 (COLUMN_A) SELECT COLUMN_B FROM T2;

In this statement, the decoded data types for COLUMN_A and COLUMN_B must be compatible. If so, the value in COLUMN_B will be decoded with F2. The decoded value is then encoded by F1, and the resulting value is inserted into COLUMN_A.


[ Top of Page | Previous Page | Next Page | Table of Contents | Index ]