An embedded SQL program consists of code written in an ordinary programming language, in this case COBOL, mixed with SQL commands in specially marked sections. To build the program, the source code (*.pco) is first passed through the embedded SQL preprocessor, which converts it to an ordinary COBOL program (*.cob), and afterwards it can be processed by a COBOL compiler. (For details about the compiling and linking see " D.9 Processing Embedded SQL Programs" ). Converted ECOBPG applications call functions in the libpq library through the embedded SQL library (ecpglib), and communicate with the PostgreSQL server using the normal frontend-backend protocol.
Embedded SQL has advantages over other methods for handling SQL commands from COOBL code. First, it takes care of the tedious passing of information to and from variables in your C program. Second, the SQL code in the program is checked at build time for syntactical correctness. Third, embedded SQL in COBOL is specified in the SQL standard and supported by many other SQL database systems. The PostgreSQL implementation is designed to match this standard as much as possible, and it is usually possible to port embedded SQL programs written for other SQL databases to PostgreSQL with relative ease.
As already stated, programs written for the embedded SQL interface are normal COBOL programs with special code inserted to perform database-related actions. This special code always has the form:
EXEC SQL ... END-EXEC
These statements syntactically take the place of a COBOL statement. Depending on the particular statement, they can appear at the data division or at the procedure division. Actual executable SQLs need to be placed at the procedure division, and host variable declarations need to be placed at data division. However, the precompiler does not validate their placements. Embedded SQL statements follow the case-sensitivity rules of normal SQL code, and not those of COBOL.
The precompiler introduces fixed syntax for embedded SQL in COBOL. In each line, 1st column to 6th column constitute line number area, and 7th column do indicator area. Embedded SQL programs also should be placed on the area B(12-72 column).
Note that sample codes in this document ommitt indents for each area.
ECOBPG processes and outputs programs compliance for fixed syntax. However, there are a few restrictions for using ecobps as follows.
Ecobpg does not validate the limitation of number of characters other than embedded SQLs. 73 and later columns are deleted in the precompiled source.
ECOBPG accepts generally possible COBOL statement. However, there are a few restrictions for using ecobps as follows.
In declaring host variable section, you can't use debug line.
Outside of declaring host variable section, you can use debug line, but you can't contain any SQL in debug lines.
In declaring host variable section, you can't use commas or semicolons as separator. Use space instead.
EXEC SQL VAR command, it can be used in ECPG, is not available in ECOBPG. Use REDEFINE clause of COBOL insetead.
The following sections explain all the embedded SQL statements.