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.
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 outputcobol
Read the code. What exactly will it print? Commit to an answer before you look.
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.
NULL Values: Relational databases support NULL, but COBOL memory does not natively have a concept of “null” numeric or string data. To handle NULL values, you must provide a secondary “indicator variable” (a PIC S9(4) COMP field) alongside your host variable in the SQL statement. If the indicator is -1, the database value was NULL.
String Continuation: If a long SQL string literal spans multiple lines, you must follow strict COBOL line continuation formatting, usually by placing a hyphen (-) in column 7 of the continued line.
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.
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.