SQL tables

SQL supports tables that contain Unicode graphic columns by specifying a Unicode CCSID for the GRAPHIC and VARGRAPHIC data types.

The following SQL example creates the table U_TABLE. U_TABLE contains one character column called EMPNO, and two Unicode graphic columns. NAME is a fixed-length Unicode graphic column and DESCRIPTION is a variable-length Unicode graphic column. The decision was made to use a character field because the EMPNO field only contains numerics and Unicode support is not needed. The NAME and DESCRIPTION fields are both Unicode fields. Both of these fields may contain data from more than one EBCDIC code page.

CREATE TABLE U_TABLE (EMPNO CHAR(6) NOT NULL,
NAME GRAPHIC(30) CCSID 1200,
DESCRIPTION VARGRAPHIC(500) CCSID 1200)

Inserting data

SBCS character, mixed character, and DBCS graphic data can be inserted into Unicode graphic columns using the SQL INSERT statement. Db2® for i SQL converts the data to Unicode graphic data. In SQL programs, the DECLARE VARIABLE statement can be used to attach a Unicode CCSID to graphic host variables.

The following SQL example converts character data to Unicode graphic data for the NAME and DESCRIPTION columns and inserts the row into the U_TABLE.

INSERT INTO U_TABLE VALUES('000001','John Doe','Engineer')

Selecting Unicode data

Implicit conversion of Unicode graphic data is supported on a FETCH or select INTO and CALL.

In the following example, the EMPNO column is returned in empno_hv as character data. The NAME column is returned in name_hv as Unicode graphic data because name_hv is a Unicode variable. It is not converted to character, mixed character, or DBCS graphic.

...
char empno_hv[7];
wchar_t name_hv[31];
EXEC SQL DECLARE :name_hv VARIABLE CCSID 13488;
...
EXEC SQL SELECT EMPNO, NAME
INTO :empno_hv, :name_hv
.FROM U_TABLE;
...

To return Unicode graphic data as EBCDIC data, the prior example can be changed to return the Unicode data as character data, EMPNO and NAME are returned in the job CCSID.

...
char empno_hv[7];
char name_hv[31];
...
EXEC SQL SELECT EMPNO, NAME
INTO :empno_hv, :name_hv
FROM U_TABLE;
...

When doing selection, implicit conversions are done when comparing Unicode graphic data and character or DBCS graphic data.

The following example converts the character string 'John Doe' to Unicode graphic and then selects the rows where the NAME column is 'John Doe'.

EXEC SQL DECLARE C1 CURSOR FOR
SELECT *
FROM U_TABLE
WHERE NAME = 'John Doe';

The SQL Reference information includes additional information about using SQL with Unicode graphic data.