Controlling the CCSID for COBOL host variables

Setting the CCSID for COBOL host variables is slightly different than the process for other host languages. In COBOL, several other settings affect the CCSID.

Before you begin

This task applies to programs that use IBM® Enterprise COBOL for z/OS® and the DB2® coprocessor.

Procedure

To control the CCSID for COBOL host variables:

Use one or more of the following items:
The NATIONAL data type
Use this data type to declare Unicode values in the UTF-16 format (CCSID 1200).
If you declare a host variable HV1 as USAGE NATIONAL, DB2 always handles HV1 as if you had used the following DECLARE VARIABLE statement:
DECLARE :HV1 VARIABLE CCSID 1200
The COBOL CODEPAGE compiler option
Use this option to specify the default EBCDIC CCSID of character data items.
The SQLCCSID compiler option
Use this option to control whether the CODEPAGE compiler option influences the processing of SQL host variables in your COBOL programs (available in Enterprise COBOL V3R4 or later).

When you specify the SQLCCSID compiler option, the COBOL DB2 coprocessor uses the CCSID that is specified in the CODEPAGE compiler option. All host variables of character data type, other than NATIONAL, are specified with that CCSID unless they are explicitly overridden by a DECLARE VARIABLE statement.

When you specify the NOSQLCCSID compiler option, the CCSID that is specified in the CODEPAGE compiler option is used for processing only COBOL statements within the COBOL program. That CCSID is not used for the processing of host variables in SQL statements. DB2 uses the CCSIDs that are specified through DB2 mechanisms and defaults as host variable data value encodings.

The DECLARE VARIABLE statement.
This statement explicitly sets the CCSID for individual host variables.

Example

Assume that the COBOL SQLCCSID compiler option is specified and that the COBOL CODEPAGE compiler option is specified as CODEPAGE(1141). The following code shows how you can control the CCSID:
DATA DIVISION.
     01  HV1  PIC N(10) USAGE NATIONAL.
     01  HV2  PIC X(20) USAGE DISPLAY.
     01  HV3  PIC X(30) USAGE DISPLAY.
     ...
     EXEC SQL
       DECLARE :HV3 VARIABLE CCSID 1047
     END-EXEC.
     ...
PROCEDURE DIVISION.
     ...
     EXEC SQL
       SELECT C1, C2, C3 INTO :HV1, :HV2, :HV3 FROM T1 
     END-EXEC. 
Each of the host variables have the following CCSIDs:
HV1
1200
HV2
1141
HV3
1047
Assume that the COBOL NOSQLCCSID compiler option is specified, the COBOL CODEPAGE compiler option is specified as CODEPAGE(1141), and the DB2 default single byte CCSID is set to 37. In this case, each of the host variables in this example have the following CCSIDs:
HV1
1200
HV2
37
HV3
1047