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.
Related terms
- SQLThe language used to query and change relational data. The same SQL used everywhere else, running against mainframe tables.
- DB2 for z/OSIBM's relational database on the mainframe, where a great deal of the world's banking and insurance data is stored.
- COBOLThe programming language most business logic on the mainframe is written in. Verbose by design, and still running the world's payments.
- BINDPreparing a program's SQL for execution, working out how each statement will access the data and storing the result.
- SQLCODEThe result code DB2 returns after every SQL statement, saying whether it worked and what happened if it did not.
- CursorA mechanism for stepping through a set of rows one at a time, when a query returns more than one result.
- DCLGENA tool that generates COBOL declarations matching a DB2 table, so the program's fields line up with the columns.