Specifying Column Lengths in Dynamic SQL

Perhaps the biggest effort in getting COBOL fully functional in a Unicode environment is setting up the bind parameters and select buffers of any dynamic SQL statements.

Programs that use dynamic SQL must specify the column lengths for bind or select fields before calling the PTPDYSQL program. Within a COBOL program, there are two ways that you can assign bind parameters and select buffers of dynamic SQL statements:

  • By using a predefined working storage area with the dynamic SQL statement.

    This method is similar to the method that is used for stored SQL statements. In this case, PTPDYSQL adjusts the length of character-data fields that are passed to PTPSQLRT. This is necessary because the COBOL Unicode conversion utility expands only the working storage fields; it does not modify the length of fields that are hard-coded in the PROGRAM-DIVISION section of the COBOL programs.

    Because PTPDYSQL sends the correct length to PTPSQLRT, no changes to the COBOL program are necessary.

  • By using a buffer array.

    At runtime, this array is partitioned based on the properties of all of the fields that are referenced by the dynamic SQL statement. The properties of those fields are retrieved from the PSDBFIELD table, and include both the field’s data type and the field’s length.

    In this case, you must modify the COBOL program to adjust the length that is specified for a character field. Adjust the length by a factor of three.

To adjust the length of the character field appropriately, the program must recognize the encoding scheme that is used by the COBOL API. The program can take advantage of the ENCODING-MODE-SW field in PTCSQLRT to determine when the length of the field needs to be adjusted.

This example illustrates the use of a buffer array to calculate the length of a character field in the Unicode environment:

02  SELECT-DATA.
               03  FIELDNAME           PIC X(54)   VALUE SPACE.
               03  FIELDNUM            PIC 9(3)    VALUE ZERO  COMP.
               03  DEFFIELDNAME        PIC X(90)   VALUE SPACES.
               03  FIELDLEN            PIC 9(3)    VALUE ZERO  COMP.
               03  FIELDTYPE           PIC 9(2)    VALUE ZERO  COMP.
                   88  RDM-CHAR                    VALUE ZERO.
                   88  RDM-LONG-CHAR               VALUE 1.
                   88  RDM-NUMBER                  VALUE 2.
                   88  RDM-SIGNED-NUMBER           VALUE 3.
                   88  RDM-DATE                    VALUE 4.
                   88  RDM-TIME                    VALUE 5.
                   88  RDM-DATETIME                VALUE 6.
               03  DECIMALPOS          PIC 9(2)    VALUE ZERO  COMP.
               03  FILLER              PIC X       VALUE 'Z'.
                   88  RDM-NUMBER                  VALUE 2.
                   88  RDM-SIGNED-NUMBER           VALUE 3.
                   88  RDM-DATE                    VALUE 4.
                   88  RDM-TIME                    VALUE 5.
                   88  RDM-DATETIME                VALUE 6.
               03  DECIMALPOS          PIC 9(2)    VALUE ZERO  COMP.
               03  FILLER              PIC X       VALUE 'Z'.
       . . .
MOVE CORR SELECT-DATA OF S-RECFLD
                        TO FLD-ARRAY OF RECFLD (FLD-IDX)
            *  CONVERT FIELD TYPE FROM PSDBFIELD TYPE TO SQLRT CODE.
MOVE FIELDLEN OF S-RECFLD TO SETUP-LENGTH OF RECFLD (FLD-IDX)
IF RDM-CHAR OF S-RECFLD
           SET SETUP-TYPE-CHAR OF RECFLD (FLD-IDX) TO TRUE
                     IF UNICODE-MODE OF SQLRT
                 COMPUTE SETUP-LENGTH OF RECFLD (FLD-IDX) =
                      SETUP-LENGTH OF RECFLD (FLD-IDX) * 3
           END-IF
           END-IF