BEGIN DECLARE SECTION
The BEGIN DECLARE SECTION statement marks the beginning of an SQL declare section. An SQL declare section contains declarations of host variables that are eligible to be used as host variables in SQL statements in a program.
Invocation
This statement can only be embedded in an application program. It is not an executable statement. It must not be specified in Java™, RPG, or REXX.
Authorization
None required.
Syntax
Description
The BEGIN DECLARE SECTION statement is used to indicate the beginning of an SQL declare section. It can be coded in the application program wherever variable declarations can appear in accordance with the rules of the host language. It cannot be coded in the middle of a host structure declaration. An SQL declare section ends with an END DECLARE SECTION statement, described in END DECLARE SECTION.
The BEGIN DECLARE SECTION and the END DECLARE SECTION statements must be paired and cannot be nested.
SQL statements should not be included within a declare section, with the exception of the DECLARE VARIABLE and INCLUDE statements.
If SQL declare sections are specified in the program, only the variables declared within the SQL declare sections can be used as host variables. If SQL declare sections are not specified in the program, all variables in the program are eligible for use as host variables.
SQL declare sections should be specified for host languages, other than RPG and REXX, so that the source program conforms to the IBM® SQL standard of SQL. SQL declare sections are required for all host variables in C++. The SQL declare section should appear before the first reference to the variable. Host variables are declared without the use of these statements in Java and RPG, and they are not declared at all in REXX.
Variables declared outside an SQL declare section should not have the same name as variables declared within an SQL declare section.
More than one SQL declare section can be specified in the program.
Examples
Example 1: Define the host variables hv_smint (SMALLINT), hv_vchar24 (VARCHAR(24)), and hv_double (DOUBLE) in a C program.
EXEC SQL BEGIN DECLARE SECTION;
static short hv_smint;
static struct {
short hv_vchar24_len;
char hv_vchar24_value[24];
} hv_vchar24;
static double hv_double;
EXEC SQL END DECLARE SECTION;
Example 2: Define the host variables HV-SMINT (smallint), HV-VCHAR24 (varchar(24)), and HV-DEC72 (dec(7,2)) in a COBOL program.
WORKING-STORAGE SECTION.
EXEC SQL BEGIN DECLARE SECTION END-EXEC.
01 HV-SMINT PIC S9(4) BINARY.
01 HV-VCHAR24.
49 HV-VCHAR24-LENGTH PIC S9(4) BINARY.
49 HV-VCHAR24-VALUE PIC X(24).
01 HV-DEC72 PIC S9(5)V9(2) PACKED-DECIMAL.
EXEC SQL END DECLARE SECTION END-EXEC.