Problem: How will the COBOL-DB2 program behave when there is a mismatch between the host variable and number of columns in the SELECT statement?
In case there is a mismatch in the number of columns and number of host variables, the query will fail. For example, if we have used the below query in a COBOL-DB2 program which processes the ORDERS DB2 table.
EXEC SQL SELECT ORDER_ID, ORDER_AMOUNT, ORDER_DATE, ORDER_STATUS INTO :WS-ORDER-ID, :WS-ORDER-AMOUNT, :WS-ORDER-DATE, FROM ORDERS WHERE ORDER_DATE = ‘2020-09-15’ END-EXEC
There is a mismatch in the number of columns and host variables. There are a total 4 columns used in the SELECT statement and for them only 3 host variables are used. In this case the query will fail and there are two ways in which we can detect this condition.
We can use IF condition to check the value in SQLWARN3 or SQLCODE and direct the program processing accordingly.
Following is an example.
IF SQLWARN3 = ‘W’ PERFORM X00-ABEND-SECTION ELSE PERFORM A100-SELECT-RESULT END-IF