Setting the CCSID for host variables
All Db2 string data, other than binary data, has an encoding scheme and a coded character set ID (CCSID) associated with it. You can associate an encoding scheme and a CCSID with individual host variables. Any data in those host variable is then associated with that encoding scheme and CCSID.
Procedure
To set the CCSID for host variables:
Specify the DECLARE VARIABLE statement after the corresponding
host variable declaration and before your first reference to that
host variable.
This statement associates an encoding scheme and a CCSID with individual host variables. You can use this statement in static or dynamic SQL applications.
Restriction: You cannot use the DECLARE VARIABLE statement to
control the CCSID and encoding scheme of data that you retrieve or
update by using an SQLDA.
The DECLARE
VARIABLE statement has the following effects on a host variable:
- When you use the host variable to update a table, the local subsystem or the remote server assumes that the data in the host variable is encoded with the CCSID and encoding scheme that the DECLARE VARIABLE statement assigns.
- When you retrieve data from a local or remote table into the host variable, the retrieved data is converted to the CCSID and encoding scheme that are assigned by the DECLARE VARIABLE statement.
Example
Suppose that you are writing a C program that runs on
a Db2 for z/OS® subsystem.
The subsystem has an EBCDIC application encoding scheme. The C program
retrieves data from the following columns of a local table that is
defined with the CCSID UNICODE option:
PARTNUM CHAR(10)
JPNNAME GRAPHIC(10)
ENGNAME VARCHAR(30) Because the application encoding scheme
for the subsystem is EBCDIC, the retrieved data is EBCDIC. To make
the retrieved data Unicode, use DECLARE VARIABLE statements to specify
that the data that is retrieved from these columns is encoded in the
default Unicode CCSIDs for the subsystem. Suppose that you
want to retrieve the character data in Unicode CCSID 1208 and the
graphic data in Unicode CCSID 1200. Use the following DECLARE VARIABLE
statements:
EXEC SQL BEGIN DECLARE SECTION;
char hvpartnum[11];
EXEC SQL DECLARE :hvpartnum VARIABLE CCSID 1208;
sqldbchar hvjpnname[11];
EXEC SQL DECLARE :hvjpnname VARIABLE CCSID 1200;
struct {
short len;
char d[30];
} hvengname;
EXEC SQL DECLARE :hvengname VARIABLE CCSID 1208;
EXEC SQL END DECLARE SECTION;