Embedded SQL statements in COBOL applications
You can include embedded
SQL statements in COBOL applications. Before you can use the SQL statements,
you must set up and enable your application to support embedded SQL.
Embedded SQL statements in COBOL applications consist of the following
three elements:
- Correct COBOL Element Syntax
- Statement initializer
- EXEC SQL
- Statement string
- Any valid SQL statement
- Statement terminator
- END-EXEC.
For example:
EXEC SQL SELECT col INTO :hostvar FROM table END-EXEC. The following rules apply to embedded SQL statements in COBOL applications:
- Executable SQL statements must be placed in the
PROCEDURE DIVISIONsection. The SQL statements can be preceded by a paragraph name, just as a COBOL statement. - SQL statements can begin in either
Area A(columns 8 through 11) orArea B(columns 12 through 72). - Start each SQL statement with the statement initializer EXEC SQL and end it with the statement terminator END-EXEC. The SQL precompiler includes each SQL statement as a comment in the modified source file.
- You must use the SQL statement terminator. If you do not use it, the precompiler will continue to the next terminator in the application. This may cause indeterminate errors.
- SQL comments are allowed on any line that is part of an embedded
SQL statement. These comments are not allowed in dynamically executed
statements. The format for an SQL comment is a double dash (
--), followed by a string of zero or more characters and terminated by a line end. Do not place SQL comments after the SQL statement terminator as they will cause compilation errors because they seem to be part of the COBOL language. - COBOL comments are allowed in most places. The exceptions are:
- Comments are not allowed between EXEC and SQL.
- Comments are not allowed in dynamically executed statements.
- SQL statements follow the same line continuation rules as the COBOL language. However, do not split the EXEC SQL statement initializer between lines.
- Do not use the COBOL COPY statement to include files containing SQL statements. SQL statements are precompiled before the module is compiled. The precompiler will ignore the COBOL COPY statement. Instead, use the SQL INCLUDE statement to import the include files.
- To continue a string constant to the next line, column 7 of the continuing line must contain a '-' and column 12 or beyond must contain a string delimiter.
- SQL arithmetic operators must be delimited by blanks.
- Substitution of white space characters, such as end-of-line and
TAB characters, occurs as follows:
- When they occur outside quotation marks (but inside SQL statements), end-of-lines and TABs are substituted by a single space.
- When they occur inside quotation marks, the end-of-line characters disappear, provided the string is continued properly for a COBOL program. TABs are not modified.
Note that the actual characters used for end-of-line and TAB vary from platform to platform. For example, Windows-based platforms use Carriage Return/Line Feed for end-of-line, whereas UNIX and Linux® based systems use just a Line Feed.