Variable and Type Declarations
Embedded SQL statements use COBOL data items, also called variables, to transfer data from the database or a form into the program and conversely. You must declare COBOL data items to SQL before using them in any embedded SQL statements.
Embedded SQL Variable Declaration Sections
Declare COBOL data items to SQL in a declaration section. This section has the following syntax:
exec sql begin declare section end-exec
COBOL variable declarations
exec sql end declare section end-exec
Place the declaration section in either the File or Working‑Storage Section of the Data Division.
Embedded SQL variable declarations are global to the program file from the point of declaration onwards. You can incorporate multiple declaration sections into a single file when, for example, multiple COBOL programs appear in the same file. Each program can have its own declaration section. For more information, see
The Scope of Variables in this chapter.
Data Item Declaration Syntax
This section describes rules and restrictions for declaring COBOL data items in embedded SQL declaration sections. All data items in a declaration section must be declared with the correct syntax. Embedded SQL recognizes only a subset of legal COBOL declarations.
The following template is the complete data item declaration format that embedded SQL accepts:
level-number
[data-name | FILLER]
[ REDEFINES data-item]
[ [IS] GLOBAL]
[ [IS] EXTERNAL]
[ PICTURE [IS] pic-string ]
[ [USAGE [IS]] use-type ]
[ SIGN clause ]
[ SYNCHRONIZED clause ]
[ JUSTIFIED clause ]
[ BLANK clause ]
[ VALUE clause ]
[ OCCURS clause ]
Syntax Notes:
• Data declaration clauses can be in any order, with the following two exceptions:
– The data-name or FILLER clause, if given, must immediately follow the level number.
– The REDEFINES clause, if given, must immediately follow the data‑item or FILLER clause.
• The level-number can range from 01 to 49. Level number 77 (for noncontiguous data items) is also valid and the preprocessor regards it as identical to level 01. The embedded SQL preprocessor does not support Level 66 (which identifies RENAMES items) and Level 88 (which associates condition names with values).
Follow the COBOL rules for specifying the organization of data when you assign level numbers to your embedded SQL data items. Like the COBOL compiler, the preprocessor recognizes that a data item belongs to a record or group if its level number is greater than the record or group level number.
• The data-name must begin with an alphabetic character, which can be followed by alphanumeric characters, hyphens, and underscores. The word FILLER can appear in place of data-name; however, you cannot explicitly reference a FILLER item in an embedded SQL statement. If the data‑name or FILLER clause is omitted, FILLER is the default.
• The preprocessor accepts but does not use the REDEFINES, GLOBAL, EXTERNAL, SIGN, SYNCHRONIZED, JUSTIFIED, BLANK, and VALUE clauses. Consequently, illegal use of these clauses goes undetected at preprocessing time but generates COBOL errors later at compile time. For example, the preprocessor does not check that a GLOBAL clause appears only on an 01 level item, nor that a SIGN clause appears only on a numeric item.
• The preprocessor expects a PICTURE clause on the COMP, COMP‑3, COMP‑5 (UNIX), and DISPLAY use‑types.
• Do not use a PICTURE clause on COMP‑1 (VMS), COMP‑2 (VMS), and INDEX use‑types.
Although the preprocessor recognizes all the valid COBOL PICTURE symbols, it only makes use of the type and size information needed for runtime support. It does not, for instance, complain about certain illegal combinations of editing symbols in picture strings. Embedded SQL accepts PIC as an abbreviation for PICTURE. You must specify the picture string on the same line as the keyword PICTURE.
• For information on the valid
use‑types for the USAGE clause and their interaction with picture strings, see
Data Types in this chapter.
• The preprocessor accepts the OCCURS clause for all data items in the level range 02 through 49. The preprocessor does not use the information in the OCCURS clause, except to note that the item described is an array. If you use an OCCURS clause on level 01, the preprocessor issues an error but generates correct code so that you can compile and link the program.
Reserved Words in Declarations
The ESQL/COBOL words in the following table are reserved when used in the DECLARE section. Additionally, the words with an asterisk are also reserved wherever they are used because they have the same name as embedded SQL keywords.
You cannot declare data items with the same name as the words that do not have an asterisk and you can only use them in quoted string constants. However, the asterisked words that match ESQL keywords can have data items with the same name.