How to truncate trapping of a DB2 column data when assigned to a host variable


There are situations in which DCLGEN members are not used and the host variables declarations are done explicitly in the working storage section. However, due to these explicit declarations there are chances of human errors. One such error is declaring incorrect data length of COBOL equivalent host variables.

For example, the host variable should have been declared as PIC X(24) but it was declared as PIC X(14) by mistake. In this case when the data transfer takes place from DB2 to COBOL program, the column data might get truncated due to the shorter length of the receiving host variable.

We can detect such situations using SQLWARN1 field of SQLCA. The SQLWARN1 is 1 byte field which contains the value ‘W’ if there is any truncation while data transfer. We can make use of this field as below.

A010-CHECK-ORDER.
EXEC SQL
SELECT ORDER_DATE
INTO :ORDER-DATE,
FROM ORDERS
WHERE ORDER_ID = :ORDER-ID
END-EXEC
EVALUATE SQLWARN1
WHEN ‘W’
DISPLAY ‘THE ORDER DATE WAS TRUNCATED’
WHEN OTHER
DISPLAY ‘ORDER DATE IS ‘ ORDER-DATE

Updated on: 14-Sep-2020

127 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements