DB2 Equivalent COBOL Data Types
As DB2/SQL and COBOL field declarations are different, It is important to have Equivalanet COBOL data types to the corresponding DB2 table columns in COBOL programs to process the data correctly. More importantly, the precompiler uses the equivalent SQL data types for the declared Host variables. So if the host variable declaration is not correct, we might face issues while executing the programs.
Below are the compatible COBOL and SQL data types.
DB2/SQL Data Tppe | COBOL Equivalent PIC Clause |
INTEGER | PIC S9(08) COMP |
SMALLINT | PIC S9(04) COMP |
DATE | PIC X(10) |
TIME | PIC X(08) |
TIMESTAMP | PIC X(26) |
CHAR(n) | PIC X(n) |
DECIMAL(x,y) | PIC S9(x-y)V9(y) |
VARCHAR(n) | 01 WS-GRP-VAR. 49 WS-LENGTH PIC S9(04) COMP. 49 WS-VAR PIC X(n) |
Special care needs to be taken while processing the VARCHAR data in host programs. A group variable consisting of Length and Actual variable as the sub fields needs to be declared. Without the length variable, data gets truncated and inappropriate results encounter.
Refer the below IBM link for more details.
For SMALLINT, INTEGER, and BIGINT, it is better to use COMP-5 fields. When using COMP-5, COBOL does not truncate the high-order digit to match the picture clause.
Example, when a DB2 column is smallint_nbr = 32000.
smallint-comp will return 2000.
smallint-comp5 will return 32000.