IBM Books

Application Development Guide


Supported SQL Data Types

Certain predefined C and C++ data types correspond to the database manager column types. Only these C/C++ data types can be declared as host variables.

Table 19 shows the C/C++ equivalent of each column type. When the precompiler finds a host variable declaration, it determines the appropriate SQL type value. The database manager uses this value to convert the data exchanged between the application and itself.
Note:There is no host variable support for the DATALINK data type in any of the DB2 host languages.

Table 19. SQL Data Types Mapped to C/C++ Declarations
SQL Column Type(TNTA1) C/C++ Data Type SQL Column Type Description

SMALLINT
(500 or 501)


short
short int

16-bit signed integer

INTEGER
(496 or 497)


long
long int

32-bit signed integer

BIGINT
(492 or 493)


long long
__int64
sqlint64(TNTA1A)

64-bit signed integer

REAL(TNTA2)
(480 or 481)

float Single-precision floating point

DOUBLE(TNTA3)
(480 or 481)

double Double-precision floating point

DECIMAL(p,s)
(484 or 485)

No exact equivalent; use double Packed decimal

(Consider using the CHAR and DECIMAL functions to manipulate packed decimal fields as character data.)


CHAR(1)
(452 or 453)

char Single character

CHAR(n)
(452 or 453)

No exact equivalent; use char[n+1] where n is large enough to hold the data
1<=n<=254
Fixed-length character string

VARCHAR(n)
(448 or 449)


struct tag {
short int;
char[n]
}
 
1<=n<=32 672

Non null-terminated varying character string with 2-byte string length indicator
Alternately use char[n+1] where n is large enough to hold the data
1<=n<=32 672
null-terminated variable-length character string
Note:Assigned an SQL type of 460/461.

LONG VARCHAR
(456 or 457)


struct tag {
short int;
char[n]
}
 
32 673<=n<=32 700

Non null-terminated varying character string with 2-byte string length indicator

CLOB(n)
(408 or 409)


sql type is
clob(n)
 
1<=n<=2 147 483 647

Non null-terminated varying character string with 4-byte string length indicator

CLOB locator variable(TNTA4)
(964 or 965)


sql type is
clob_locator

Identifies CLOB entities residing on the server

CLOB file reference variable(TNTA4)
(808 or 809)


sql type is
clob_file

Descriptor for file containing CLOB data

BLOB(n)
(404 or 405)


sql type is
blob(n)
 
1<=n<=2 147 483 647

Non null-terminated varying binary string with 4-byte string length indicator

BLOB locator variable(TNTA4)
(960 or 961)


sql type is
blob_locator

Identifies BLOB entities on the server

BLOB file reference variable(TNTA4)
(804 or 805)


sql type is
blob_file

Descriptor for the file containing BLOB data

DATE
(384 or 385)

null-terminated character form Allow at least 11 characters to accommodate the null-terminator.
VARCHAR structured form Allow at least 10 characters.

TIME
(388 or 389)

null-terminated character form Allow at least 9 characters to accommodate the null-terminator.
VARCHAR structured form Allow at least 8 characters.

TIMESTAMP
(392 or 393)

null-terminated character form Allow at least 27 characters to accommodate the null-terminator.
VARCHAR structured form Allow at least 26 characters.
Note:The following data types are only available in the DBCS or EUC environment when precompiled with the WCHARTYPE NOCONVERT option.

GRAPHIC(1)
(468 or 469)

sqldbchar Single double-byte character

GRAPHIC(n)
(468 or 469)

No exact equivalent; use sqldbchar[n+1] where n is large enough to hold the data
1<=n<=127
Fixed-length double-byte character string

VARGRAPHIC(n)
(464 or 465)


struct tag {
short int;
sqldbchar[n]
}
 
1<=n<=16 336

Non null-terminated varying double-byte character string with 2-byte string length indicator
Alternately use sqldbchar[n+1] where n is large enough to hold the data
1<=n<=16 336
null-terminated variable-length double-byte character string
Note:Assigned an SQL type of 400/401.

LONG VARGRAPHIC
(472 or 473)


struct tag {
short int;
sqldbchar[n]
}
 
16 337<=n<=16 350

Non null-terminated varying double-byte character string with 2-byte string length indicator
Note:The following data types are only available in the DBCS or EUC environment when precompiled with the WCHARTYPE CONVERT option.

GRAPHIC(1)
(468 or 469)

wchar_t
  • Single wide character (for C-type)
  • Single double-byte character (for column type)


GRAPHIC(n)
(468 or 469)

No exact equivalent; use wchar_t [n+1] where n is large enough to hold the data
1<=n<=127
Fixed-length double-byte character string

VARGRAPHIC(n)
(464 or 465)


struct tag {
short int;
wchar_t [n]
}
 
1<=n<=16 336

Non null-terminated varying double-byte character string with 2-byte string length indicator
Alternately use char[n+1] where n is large enough to hold the data
1<=n<=16 336
null-terminated variable-length double-byte character string
Note:Assigned an SQL type of 400/401.

LONG VARGRAPHIC
(472 or 473)


struct tag {
short int;
wchar_t [n]
}
 
16 337<=n<=16 350

Non null-terminated varying double-byte character string with 2-byte string length indicator
Note:The following data types are only available in the DBCS or EUC environment.

DBCLOB(n)
(412 or 413)


sql type is
dbclob(n)
 
1<=n<=1 073 741 823

Non null-terminated varying double-byte character string with 4-byte string length indicator

DBCLOB locator variable(TNTA4)
(968 or 969)


sql type is
dbclob_locator

Identifies DBCLOB entities residing on the server

DBCLOB file reference
variable(TNTA4)
(812 or 813)


sql type is
dbclob_file

Descriptor for file containing DBCLOB data

Notes:

  1. (TNTA1) The first number under SQL Column Type indicates that an indicator variable is not provided, and the second number indicates that an indicator variable is provided. An indicator variable is needed to indicate NULL values, or to hold the length of a truncated string. These are the values that would appear in the SQLTYPE field of the SQLDA for these data types.

  2. (TNTA1A) Windows operating systems use __int64, where __ represents 2 underscores. For platform compatibility, use sqlint64. The DB2 Universal Database sqlsystm.h header file will type define sqlint64 as "__int64" on the Windows NT platform when using the Microsoft compiler; otherwise, sqlint64 will be type defined as "long long".

  3. (TNTA2) FLOAT(n) where 0 < n < 25 is a synonym for REAL. The difference between REAL and DOUBLE in the SQLDA is the length value (4 or 8).

  4. (TNTA3) The following SQL types are synonyms for DOUBLE:
    • FLOAT
    • FLOAT(n) where 24 < n < 54 is
    • DOUBLE PRECISION

  5. (TNTA4) This is not a column type but a host variable type.

The following is a sample SQL declare section with host variables declared for supported SQL data types.

   EXEC SQL BEGIN DECLARE SECTION; 
   ... 
        short     age = 26;               /* SQL type  500 */ 
        short     year;                   /* SQL type  500 */ 
        long      salary;                 /* SQL type  496 */ 
        long      deptno;                 /* SQL type  496 */ 
        float     bonus;                  /* SQL type  480 */
        double    wage;                   /* SQL type  480 */ 
        char      mi;                     /* SQL type  452 */ 
        char      name[6];                /* SQL type  460 */ 
        struct   { 
                  short len; 
                  char data[24]; 
                 } address;               /* SQL type  448 */ 
        struct   { 
                  short len; 
                  char data[5764]; 
                 } voice;                 /* SQL type  456 */ 
        sql type is clob(1m) 
                  chapter;                /* SQL type  408 */ 
        sql type is clob_locator 
                  chapter_locator;        /* SQL type  964 */ 
        sql type is clob_file 
                  chapter_file_ref;       /* SQL type  808 */ 
        sql type is blob(1m) 
                  video;                  /* SQL type  404 */ 
        sql type is blob_locator 
                  video_locator;          /* SQL type  960 */ 
        sql type is blob_file 
                  video_file_ref;         /* SQL type  804 */ 
        sql type is dbclob(1m) 
                  tokyo_phone_dir;        /* SQL type  412 */
        sql type is dbclob_locator 
                  tokyo_phone_dir_lctr;   /* SQL type  968 */
        sql type is dbclob_file 
                  tokyo_phone_dir_flref;  /* SQL type  812 */
        struct   { 
                  short len; 
                  sqldbchar data[100]; 
                 } vargraphic1;           /* SQL type  464 */ 
                                          /* Precompiled with
                                          WCHARTYPE NOCONVERT option */ 
        struct   { 
                  short len; 
                  wchar_t data[100]; 
                 } vargraphic2;           /* SQL type  464 */ 
                                          /* Precompiled with
                                          WCHARTYPE CONVERT option */ 
        struct   { 
                  short len; 
                  sqldbchar data[10000]; 
                 } long_vargraphic1;      /* SQL type  472 */ 
                                          /* Precompiled with 
                                          WCHARTYPE NOCONVERT option */ 
        struct   { 
                  short len; 
                  wchar_t data[10000]; 
                 } long_vargraphic2;      /* SQL type  472 */ 
                                          /* Precompiled with 
                                          WCHARTYPE CONVERT option */ 
        sqldbchar graphic1[100];          /* SQL type  468 */ 
                                          /* Precompiled with 
                                          WCHARTYPE NOCONVERT option */ 
        wchar_t   graphic2[100];          /* SQL type  468 */ 
                                          /* Precompiled with 
                                          WCHARTYPE CONVERT option */ 
        char      date[11];               /* SQL type  384 */ 
        char      time[9];                /* SQL type  388 */ 
        char      timestamp[27];          /* SQL type  392 */ 
        short     wage_ind;               /* Null indicator */ 
   ... 
   EXEC SQL END DECLARE SECTION; 

The following are additional rules for supported C/C++ data types:

FOR BIT DATA

The standard C or C++ string type 460 should not be used for columns designated FOR BIT DATA. The database manager truncates this data type when a null character is encountered. Use either the VARCHAR (SQL type 448) or CLOB (SQL type 408) structures.


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

[ DB2 List of Books | Search the DB2 Books ]