Equivalent SQL and PL/I data types
When you declare host variables in your PL/I programs, the precompiler uses equivalent SQL data types. When you retrieve data of a particular SQL data type into a host variable, you need to ensure that the host variable is of an equivalent data type.
The following table describes the SQL data type and the base SQLTYPE and SQLLEN values that the precompiler uses for host variables in SQL statements.
PL/I host variable data type | SQLTYPE of host variable 1 | SQLLEN of host variable | SQL data type |
---|---|---|---|
BIN FIXED(n) 1<=n<=15 | 500 | 2 | SMALLINT |
BIN FIXED(n) 16<=n<=31 | 496 | 4 | INTEGER |
FIXED BIN(63) | 492 | 8 | BIGINT |
DEC FIXED(p,s) 0<=p<=31 and 0<=s<=p 2 | 484 | p in byte 1, s in byte 2 | DECIMAL(p,s) |
DEC FLOAT (p) where 1 <= p <= 7 | 996/997 | 4 | DECFLOAT(16) 6 |
DEC FLOAT (p) where 8 <= p <= 16 | 996/997 | 8 | DECFLOAT(16) |
DEC FLOAT (p) where 17 <= p | 996/997 | 16 | DECFLOAT(34) |
BIN FLOAT(p) 1<=p<=21 | 480 | 4 | REAL or FLOAT(n) 1<=n<=21 |
BIN FLOAT(p) 22<=p<=53 | 480 | 8 | DOUBLE PRECISION or FLOAT(n) 22<=n<=53 |
DEC FLOAT(m) 1<=m<=6 | 480 | 4 | FLOAT (single precision) |
DEC FLOAT(m) 7<=m<=16 | 480 | 8 | FLOAT (double precision) |
CHAR(n) | 452 | n | CHAR(n) |
CHAR(n) VARYING 1<=n<=255 | 448 | n | VARCHAR(n) |
CHAR(n) VARYING n>255 | 456 | n | VARCHAR(n) |
GRAPHIC(n) | 468 | n | GRAPHIC(n) |
GRAPHIC VARYING(n) | 464 | n | VARGRAPHIC(n) |
SQL TYPE IS BINARY(n), 1<=n<=255 | 912 | n | BINARY(n) |
SQL TYPE IS VARBINARY(n), 1<=n<=32 704 | 908 | n | VARBINARY(n) |
SQL TYPE IS RESULT_SET_LOCATOR | 972 | 4 | Result set locator 3 |
SQL TYPE IS TABLE LIKE table-name AS LOCATOR | 976 | 4 | Table locator 3 |
SQL TYPE IS BLOB_LOCATOR | 960 | 4 | BLOB locator 3 |
SQL TYPE IS CLOB_LOCATOR | 964 | 4 | CLOB locator 3 |
SQL TYPE IS DBCLOB_LOCATOR | 968 | 4 | DBCLOB locator 3 |
SQL TYPE IS BLOB(n) 1≤n≤2147483647 | 404 | n | BLOB(n) |
SQL TYPE IS CLOB(n) 1≤n≤2147483647 | 408 | n | CLOB(n) |
SQL TYPE IS DBCLOB(n) 1≤n≤1073741823 4 | 412 | n | DBCLOB(n) 4 |
SQL TYPE IS XML AS BLOB(n) | 404 | 0 | XML |
SQL TYPE IS XML AS CLOB(n) | 408 | 0 | XML |
SQL TYPE IS XML AS DBCLOB(n) | 412 | 0 | XML |
SQL TYPE IS BLOB_FILE | 916/917 | 267 | BLOB file reference 3 |
SQL TYPE IS CLOB_FILE | 920/921 | 267 | CLOB file reference 3 |
SQL TYPE IS DBCLOB_FILE | 924/925 | 267 | DBCLOB file reference 3 |
SQL TYPE IS XML AS BLOB_FILE | 916/917 | 267 | XML BLOB file reference 3 |
SQL TYPE IS XML AS CLOB_FILE | 920/921 | 267 | XML CLOB file reference 3 |
SQL TYPE IS XML AS DBCLOB_FILE | 924/925 | 267 | XML DBCLOB file reference 3 |
SQL TYPE IS ROWID | 904 | 40 | ROWID |
WIDECHAR(n) | 468 | n | GRAPHIC(n) 5 |
WIDECHAR VARYING(n) | 464 | n | VARGRAPHIC(n) 5 |
The following table shows equivalent PL/I host variables for each SQL data type. Use this table to determine the PL/I data type for host variables that you define to receive output from the database. For example, if you retrieve TIMESTAMP data, you can define a variable of type CHAR(n).
This table shows direct conversions between SQL data types and PL/I data types. However, a number of SQL data types are compatible. When you do assignments or comparisons of data that have compatible data types, DB2® converts those compatible data types.
SQL data type | PL/I host variable equivalent | Remarks |
---|---|---|
SMALLINT | BIN FIXED(n) | 1<=n<=15 |
INTEGER | BIN FIXED(n) | 16<=n<=31 |
BIGINT | FIXED BIN(63) | 7 |
DECIMAL(p,s) or NUMERIC(p,s) | If p<16: DEC FIXED(p) or DEC FIXED(p,s) | p is precision; s is scale. 1<=p<=31
and 0<=s<=p If p>15, the PL/I compiler must support 31-digit decimal variables. |
DECFLOAT(16) | DEC FLOAT (p) | 1 <= p <= 7 |
DECFLOAT(16) | DEC FLOAT (p) | 8 <= p <= 16 |
DECFLOAT(34) | DEC FLOAT (p) | 17 <= p |
REAL or FLOAT(n) | BIN FLOAT(p) or DEC FLOAT(m) | 1<=n<=21, 1<=p<=21, and 1<=m<=6 |
DOUBLE PRECISION, DOUBLE, or FLOAT(n) | BIN FLOAT(p) or DEC FLOAT(m) | 22<=n<=53, 22<=p<=53, and 7<=m<=16 |
CHAR(n) | CHAR(n) | 1<=n<=255 |
VARCHAR(n) | CHAR(n) VAR | |
GRAPHIC(n) | GRAPHIC(n) or WIDECHAR(n) 2 | n refers to the number of double-byte characters, not to the number of bytes. |
VARGRAPHIC(n) | GRAPHIC(n) VARYING or WIDECHAR(n) VARYING | n refers to the number of double-byte characters, not to the number of bytes. |
BINARY(n) | SQL TYPE IS BINARY(n) | 1<=n<=255 |
VARBINARY(n) | SQL TYPE IS VARBINARY(n) | 1<=n<=32 704 |
DATE | CHAR(n) | If you are using a date exit routine, that routine determines n; otherwise, n must be at least 10. |
TIME | CHAR(n) | If you are using a time exit routine, that routine determines n. Otherwise, n must be at least 6; to include seconds, n must be at least 8. |
TIMESTAMP | CHAR(n) | n must be at least 19. To include microseconds, n must be 26; if n is less than 26, the microseconds part is truncated. |
TIMESTAMP(0) | CHAR(n) | n must be at least 19. |
TIMESTAMP(p) p > 0 | CHAR(n) | n must be at least 19. To include fractional seconds, n must be 20+x where x is the number of fractional seconds to include; if x is less than p, truncation occurs on the fractional seconds part. |
TIMESTAMP(0) WITH TIME ZONE | CHAR(n) VAR | n must be at least 25. |
TIMESTAMP(p) WITH TIME ZONE | CHAR(n) VAR | n must be at least 26+p. |
Result set locator | SQL TYPE IS RESULT_SET_LOCATOR | Use this data type only for receiving result sets. 3 |
Table locator | SQL TYPE IS TABLE LIKE table-name AS LOCATOR | Use this data type only in a user-defined function or stored procedure to receive rows of a transition table. 3 |
BLOB locator | SQL TYPE IS BLOB_LOCATOR | Use this data type only to manipulate data in BLOB columns. 3, 6, 7 |
CLOB locator | SQL TYPE IS CLOB_LOCATOR | Use this data type only to manipulate data in CLOB columns. 3, 6, 7 |
DBCLOB locator | SQL TYPE IS DBCLOB_LOCATOR | Use this data type only to manipulate data in DBCLOB columns. 3, 6, 7 |
BLOB(n) | SQL TYPE IS BLOB(n) | 1≤n≤2147483647 6, 7 |
CLOB(n) | SQL TYPE IS CLOB(n) | 1≤n≤2147483647 6, 7 |
DBCLOB(n) | SQL TYPE IS DBCLOB(n) | n is the number of double-byte characters. 1≤n≤1073741823 5, 7 |
XML | SQL TYPE IS XML AS BLOB(n) | 1≤n≤2147483647 |
XML | SQL TYPE IS XML AS CLOB(n) | 1≤n≤2147483647 |
XML | SQL TYPE IS XML AS DBCLOB(n) | n is the number of double-byte characters. 1≤n≤1073741823 6 |
BLOB file reference | SQL TYPE IS BLOB_FILE | Use this data type only to manipulate data in BLOB columns. 3, 6, 7 |
CLOB file reference | SQL TYPE IS CLOB_FILE | Use this data type only to manipulate data in CLOB columns. 3, 6, 7 |
DBCLOB file reference | SQL TYPE IS DBCLOB_FILE | Use this data type only to manipulate data in DBCLOB columns. 3, 6, 7 |
XML BLOB file reference | SQL TYPE IS XML AS BLOB_FILE | Use this data type only to manipulate XML data as BLOB files. 3 |
XML CLOB file reference | SQL TYPE IS XML AS CLOB_FILE | Use this data type only to manipulate XML data as CLOB files. 3 |
XML DBCLOB file reference | SQL TYPE IS XML AS DBCLOB_FILE | Use this data type only to manipulate XML data as DBCLOB files. 3 |
ROWID | SQL TYPE IS ROWID |
The following notes apply as indicated to Table 1 and Table 2.
- If a host variable includes an indicator variable, the SQLTYPE value is the base SQLTYPE value plus 1.
- If p=0, DB2 interprets it as DECIMAL(31). For example, DB2 interprets a PL/I data type of DEC FIXED(0,0) to be DECIMAL(31,0), which equates to the SQL data type of DECIMAL(31,0).
- Do not use this data type as a column type.
- n is the number of double-byte characters.
- CCSID 1200 is always assigned to WIDECHAR type host var.
- The data type conversions can be used only if the DB2 coprocessor is used, and the PL/I compiler options FLOAT(DFP) and ARCH(7) are specified.
- Specify the following compiler options when you compile your program: LIMITS(FIXEDBIN(63), FIXEDDEC(31)).