SQL with EXEC SQL

Embedded SQL

Enterprise COBOL programs talk to Db2 with EXEC SQL blocks. The SQL sits inside the COBOL source, and a precompiler turns it into CALLs before the compiler runs:

EXEC SQL SELECT SALARY INTO :WS-SALARY FROM EMPLOYEE WHERE EMP-ID = :WS-EMP-ID END-EXEC.

Host variables

Host variables are working-storage fields marked with a colon inside EXEC SQL. They carry values in and out of the database, and SQLCODE reports success or failure after each statement:

01 WS-SALARY PIC S9(7)V99 COMP-3. 01 WS-EMP-ID PIC 9(6). EXEC SQL INCLUDE SQLCA END-EXEC.

Cursors

A cursor reads a result set row by row: DECLARE it, OPEN it, FETCH into host variables in a loop, and CLOSE it. SQLCODE 100 from FETCH means the last row was read.

The code on this page was not run: embedded SQL needs a Db2 precompiler and a database.

Article author: Arthur Isaev

Banner image