COBOL and DB2: Embedded SQL

COBOL GnuCOBOL 3.1.2 (Illustrative for DB2) · ✓ verified by execution on 2026-08-16

When enterprise applications require heavy lifting against relational databases like IBM Db2, COBOL relies on Embedded SQL. This allows you to write SQL statements directly inline with your COBOL source code, bridging the gap between flat-file processing and modern relational database architectures.

However, the COBOL compiler does not natively understand SQL syntax. To make a cobol db2 program work, the code must pass through a specialized compilation pipeline.

The Precompilation Pipeline

Before your program reaches the standard COBOL compiler, it is parsed by a DB2 Precompiler (or Coprocessor). This tool scans your source code for embedded SQL statements, extracts them to build a Database Request Module (DBRM), and replaces the SQL in your code with native COBOL CALL statements to the database library.

Source Code (COBOL + SQL) Precompiler DBRM (Database Mod) Modified Code (Pure COBOL) BIND (Plan) Compiler

Can you predict what this pipeline looks like in practice? Which step must occur first before the program can become an executable load module and talk to the database?

Predict the output cobol

Read the code. What exactly will it print? Commit to an answer before you look.

       IDENTIFICATION DIVISION.
       PROGRAM-ID. PIPELINE.
       PROCEDURE DIVISION.
           DISPLAY "3. PRECOMPILE"
           DISPLAY "1. COMPILE"
           DISPLAY "4. LINK"
           DISPLAY "2. BIND"
           STOP RUN.
Output
3. PRECOMPILE
1. COMPILE
4. LINK
2. BIND

Embedded SQL Syntax in Action

All embedded SQL statements are written inline with the program, delimited by EXEC SQL and END-EXEC. Because the precompiler turns these into host-language calls to a code library, any COBOL code outside of the EXEC SQL boundaries must remain strictly standard COBOL.

To handle errors, DB2 returns status codes through a special data structure called the SQLCA (SQL Communication Area), which you include in WORKING-STORAGE. When passing data between your COBOL program and DB2, you must use Host Variables, declared inside a specialized block and prefixed with a colon (:) in SQL statements.

Here is a complete example demonstrating the syntax for includes, host variables, SELECT, INSERT, error checking, and COMMIT:

       EXEC SQL INCLUDE SQLCA END-EXEC.

       EXEC SQL BEGIN DECLARE SECTION END-EXEC.
       01  WS-EMP-NAME PIC X(50).
       EXEC SQL END DECLARE SECTION END-EXEC.

       EXEC SQL
           SELECT EMP_NAME INTO :WS-EMP-NAME
           FROM EMPLOYEE WHERE EMP_ID = 1
       END-EXEC.

       EXEC SQL
           INSERT INTO EMPLOYEE (EMP_ID, EMP_NAME)
           VALUES (2, 'ALICE')
       END-EXEC.

       IF SQLCODE < 0
           DISPLAY 'SQL ERROR: ' SQLCODE
       END-IF.

       EXEC SQL
           COMMIT
       END-EXEC.

Simulating DB2 Responses

Because embedded SQL requires a precompiler that may not be available on all environments, you can simulate DB2 responses by manually defining the SQLCA and checking its values, exactly as the generated COBOL would do.

cobol ✓ verified output
       IDENTIFICATION DIVISION.
       PROGRAM-ID. SIMSQL.
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01  SQLCA.
           05 SQLCODE PIC S9(9) COMP VALUE -818.
       PROCEDURE DIVISION.
           IF SQLCODE < 0
               DISPLAY "SQL ERROR CODE: " SQLCODE
           ELSE
               DISPLAY "SUCCESS"
           END-IF
           STOP RUN.
Output
SQL ERROR CODE: -000000818

Edge Cases

Check yourself

How does the COBOL compiler natively understand SQL?

Reveal answer

It doesn't. A preprocessor or coprocessor translates SQL into COBOL calls before standard compilation. — Standard COBOL compilers do not understand SQL. An embedded SQL preprocessor scans the source code and replaces the SQL with host-language calls to a database library.

How must SQL variables be defined to be used in COBOL?

Reveal answer

They must be declared inside an SQL DECLARE SECTION to be recognized as valid host variables by the preprocessor. — Host variables—variables that transfer data between the COBOL program and the database—must be explicitly grouped within `EXEC SQL BEGIN DECLARE SECTION END-EXEC` in some implementations.

How are SQL statements terminated in COBOL?

Reveal answer

They end with the delimiter END-EXEC. — Embedded SQL statements in COBOL always start with `EXEC SQL` and are terminated strictly by `END-EXEC`, often followed by a period if required by the surrounding COBOL syntax.

How do you reference a COBOL host variable inside an SQL statement?

Reveal answer

By prefixing it with a colon (:). — To distinguish host language variables from database column names in a SQL statement, you must prefix the host variable with a colon (:).

Challenges

🐞 Bug Hunt +15 XP

This program simulates a DB2 error by setting SQLCODE to -818 (timestamp mismatch). The developer tried to check for an error by comparing SQLCODE to the string 'ERROR', which causes a compiler warning or logic failure because SQLCODE is a signed number. Run the code to see it fail, then fix the IF statement to correctly check if SQLCODE is less than zero.

This code runs. It just does the wrong thing. Read it, find the defect, fix it — the tests below decide when you are right.

cobol ✓ solution verified at build time

COBOL has no in-browser runtime, so this one is pen-and-paper: work out what the fixed code should be, then open the solution and compare. The expected output below is real — it came from compiling the solution with GnuCOBOL when this page was built.

  • Test 1 — expects "SQL ERROR\n"
Need a hint? (−25% XP)

Use an IF statement to check if SQLCODE < 0.

Show solution (0 XP)
IDENTIFICATION DIVISION.
PROGRAM-ID. CHECKERR.
DATA DIVISION.
WORKING-STORAGE SECTION.
01  SQLCA.
    05 SQLCODE PIC S9(9) COMP VALUE -818.
PROCEDURE DIVISION.
    IF SQLCODE < 0
        DISPLAY 'SQL ERROR'
    END-IF.
    STOP RUN.

Challenge 2 +15 XP

Declare a valid COBOL group item named DCL-EMPLOYEE that will act as a host structure, containing EMP-ID (PIC X(5)). Then DISPLAY 'OK'.

cobol ✓ solution verified at build time

COBOL has no in-browser runtime, so this one is pen-and-paper: work out what the fixed code should be, then open the solution and compare. The expected output below is real — it came from compiling the solution with GnuCOBOL when this page was built.

  • Test 1 — expects "OK\n"
Need a hint? (−25% XP)

Declare the 01 level group item DCL-EMPLOYEE with a 05 level EMP-ID.

Show solution (0 XP)
IDENTIFICATION DIVISION.
PROGRAM-ID. HOSTVAR.
DATA DIVISION.
WORKING-STORAGE SECTION.
01  DCL-EMPLOYEE.
    05 EMP-ID PIC X(5).
PROCEDURE DIVISION.
    DISPLAY 'OK'.
    STOP RUN.