SQL statements in COBOL programs

You can code SQL statements in certain COBOL program sections.

The allowable sections are shown in the following table.

Table 1. Allowable SQL statements for COBOL program sections
SQL statement Program section
INCLUDE SQLIMSCA WORKING-STORAGE SECTION1 or LINKAGE SECTION
INCLUDE text-file-name PROCEDURE DIVISION or DATA DIVISION2
DECLARE CURSOR DATA DIVISION or PROCEDURE DIVISION
Other PROCEDURE DIVISION
Notes:
  1. If you use the IMS coprocessor, you can use the LOCAL-STORAGE SECTION wherever WORKING-STORAGE SECTION is listed in the table.
  2. When including host variable declarations, the INCLUDE statement must be in the WORKING-STORAGE SECTION or the LINKAGE SECTION.

You cannot put SQL statements in the DECLARATIVES section of a COBOL program.

Each SQL statement in a COBOL program must begin with EXEC SQLIMS and end with END-EXEC. The EXEC and SQLIMS keywords can be on different lines. Do not include any tokens between the two keywords EXEC and SQL except for COBOL comments, including debugging lines. Do not include SQL comments between the keywords EXEC and SQLIMS.

If the SQL statement appears between two COBOL statements, the period after END-EXEC is optional and might not be appropriate. If the statement appears in an IF…THEN set of COBOL statements, omit the ending period to avoid inadvertently ending the IF statement.

Comments: You can include COBOL comment lines (* in field 7) in SQL statements wherever you can use a blank. You cannot include COBOL comment lines between the keywords EXEC and SQLIMS. The IMS coprocessor treats the debugging lines based on the COBOL rules, which depend on the WITH DEBUGGING mode setting.

For an SQL INCLUDE statement, the IMS coprocessor treats this text as part of the COBOL program syntax.

In addition, you can include SQL comments ('--') in any embedded SQL statement.

Debugging lines: The IMS coprocessor follows the COBOL language rules regarding debugging lines.

Continuation for SQL statements: The rules for continuing a character string constant from one line to the next in an SQL statement embedded in a COBOL program are the same as those for continuing a non-numeric literal in COBOL. However, you can use either a quote or an apostrophe as the first nonblank character in area B of the continuation line. The same rule applies for the continuation of delimited identifiers and does not depend on the string delimiter option.

To conform with SQL standard, delimit a character string constant with an apostrophe, and use a quote as the first nonblank character in area B of the continuation line for a character string constant.

Continued lines of an SQL statement can be in fields 12 through 72 when using the IMS coprocessor.

Declaring segments: Your COBOL program should include the statement DECLARE TABLE to describe each segment and view the program accesses. You can use the IMS declarations generator to generate the DECLARE TABLE statements. You should include the generated members in the DATA DIVISION.

Dynamic SQL in a COBOL program: In general, COBOL programs can easily handle dynamic SQL statements. COBOL programs can handle SELECT statements if the data types and the number of fields returned are fixed. If you want to use variable-list SELECT statements, use an SQLIMSDA.

Including code: To include SQL statements or COBOL host variable declarations from a member of a partitioned data set, use the following SQL statement in the source code where you want to include the statements:

EXEC SQLIMS INCLUDE member-name END-EXEC.

Use the 'EXEC SQLIMS' and 'END-EXEC' keyword pair to include SQL statements only. COBOL statements, such as COPY or REPLACE, are not allowed.

Margins: You must code SQL statements that begin with EXEC SQLIMS in fields 12 through 72.

Names: You can use any valid COBOL name for a host variable. Do not use entry names that begin with DFS or DQF, and do not use host variable names that begin with 'SQL' or 'SQLIMS'. These names are reserved for IMS.

Sequence numbers: The source statements that the IMS coprocessor generates do not include sequence numbers.

Statement labels: You can precede executable SQL statements in the PROCEDURE DIVISION with a paragraph name, if you wish.

WHENEVER statement: The target for the GOTO clause in an SQL statement WHENEVER must be a section name or unqualified paragraph name in the PROCEDURE DIVISION.

Special COBOL considerations: The following considerations apply to programs written in COBOL:
  • In a COBOL program that uses elements in a multi-level structure as host variable names, the IMS coprocessor generates the lowest two-level names.
  • To avoid truncating numeric values, use either of the following methods:
    • Use the COMP-5 data type for binary integer host variables.
    • Specify the COBOL compiler option:
      • TRUNC(OPT) if you are certain that the data being moved to each binary variable by the application does not have a larger precision than is defined in the PICTURE clause of the binary variable.
      • TRUNC(BIN) if the precision of data being moved to each binary variable might exceed the value in the PICTURE clause.

    IMS assigns values to binary integer host variables as if you had specified the COBOL compiler option TRUNC(BIN) or used the COMP-5 data type.

  • Do not use COBOL figurative constants (such as ZERO and SPACE), symbolic characters, reference modification, and subscripts within SQL statements.
  • Observe the rules for naming SQL identifiers. However, for COBOL only, the names of SQL identifiers can follow the rules for naming COBOL words, if the names do not exceed the allowable length for the IMS object. For example, the name 1ST-TIME is a valid cursor name because it is a valid COBOL word, but the name 1_TIME is not valid because it is not a valid SQL identifier or a valid COBOL word.
  • Observe these rules for hyphens:
    • Surround hyphens used as subtraction operators with spaces. IMS usually interprets a hyphen with no spaces around it as part of a host variable name.
  • If you include an SQL statement in a COBOL PERFORM … THRU paragraph and also specify the SQL statement WHENEVER … GO, the COBOL compiler returns the warning message IGYOP3094. That message might indicate a problem. This usage is not recommended.