Skip to content
DB2 & data

Embedded SQL

SQL statements written directly inside a COBOL program, marked off so they can be prepared before compiling.

Also written EXEC SQL, host variables

Embedded SQL is how application programs talk to DB2. Rather than building query strings and calling an interface, the SQL is written into the COBOL source itself, wrapped in markers that separate it from the surrounding code.

Values pass in and out through host variables, which are ordinary COBOL fields referenced inside the SQL statement. A program can select a customer's balance straight into a working-storage field, or insert a row using values it has calculated.

Because SQL is not COBOL, the source has to be prepared before it can be compiled. A precompiler or the compiler's built-in coprocessor extracts the SQL, replaces it with calls, and produces a database request module describing what the program intends to do. That module is then bound before the program can run.

Every statement sets a result code the program should check, and disciplined checking of it after each operation is what separates robust production code from code that silently processes wrong data.

The result is business logic and data access sitting together in one readable file, which is a large part of why the model has lasted.

Browse all 115 terms

Learn this properly.

Use Embedded SQL for real in Mainframe101, in your browser, with Zed beside you. Join the waitlist.

Early access and updates. No spam, unsubscribe any time.