Module 9: CICS DB2 Integration
CICS and DB2 COBOL Example
This page puts everything together: a complete CICS COBOL program that reads a customer number from a screen, fetches the row from DB2, and displays it.
What the program does
- The operator types a customer number on the CUSTMAP screen and presses Enter.
- The program receives the map, moves the customer number to a host variable, and runs a SELECT against the CUSTOMER table.
- If the row is found, the customer name is displayed. If not, a "not found" message is displayed.
- Any SQL error branches to an error paragraph that shows the SQLCODE. The program then syncpoints and returns.
Walking through the code
- WORKING-STORAGE holds INCLUDE SQLCA, the DCLGEN host structure, and the host variables.
- 0000-MAIN receives the map, runs the SELECT, evaluates SQLCODE, and sends the map back.
- 9000-DB2-ERROR is the single place where SQL errors are handled: it shows the SQLCODE on the screen.
- EXEC CICS SYNCPOINT commits the unit of work before RETURN ends the task.
Key points to notice
- SQLCODE is checked immediately after the SELECT, before the host variable is used.
- SQLCODE +100 (not found) is treated as a normal outcome with its own message.
- There is no EXEC SQL COMMIT anywhere. The commit is EXEC CICS SYNCPOINT.
- Screen field names (CUSTNOI, CUSTNAMEO, MSGO) come from the BMS map copybook.
Example
- IDENTIFICATION DIVISION. PROGRAM-ID. CUSTINQ. DATA DIVISION. WORKING-STORAGE SECTION. EXEC SQL INCLUDE SQLCA END-EXEC. EXEC SQL INCLUDE CUSTOMER END-EXEC. 01 WS-CUST-NO PIC X(6). 01 WS-CUST-NAME PIC X(30). PROCEDURE DIVISION. 0000-MAIN. EXEC CICS RECEIVE MAP('CUSTMAP') MAPSET('CUSTSET') INTO(CUSTMAP-INPUT) END-EXEC. MOVE CUSTNOI TO WS-CUST-NO. EXEC SQL SELECT CUSTNAME INTO :WS-CUST-NAME FROM CUSTOMER WHERE CUSTNO = :WS-CUST-NO END-EXEC. EVALUATE SQLCODE WHEN 0 MOVE WS-CUST-NAME TO CUSTNAMEO MOVE ' ' TO MSGO WHEN 100 MOVE 'CUSTOMER NOT FOUND' TO MSGO WHEN OTHER PERFORM 9000-DB2-ERROR END-EVALUATE. EXEC CICS SEND MAP('CUSTMAP') MAPSET('CUSTSET') FROM(CUSTMAP-OUTPUT) ERASE END-EXEC. EXEC CICS SYNCPOINT END-EXEC. EXEC CICS RETURN END-EXEC. 9000-DB2-ERROR. MOVE SQLCODE TO ERRCODEO. MOVE 'DB2 ERROR - SEE CODE' TO MSGO.
- To prepare this program: translate, DB2 precompile, compile, bind the package into the plan named on the transaction's DB2ENTRY, link-edit, and define the program and transaction to CICS.
