Updated on 2024-06-03 GMT+08:00

DECLARE Section

To implement data interaction between a C program with embedded SQL statements and a database, for example, to pass parameters in a query of the C program to the database, or to pass data from the database back to the program, the C variables that are intended to contain this data need to be declared in specially marked sections, so that the embedded SQL preprocessor is made aware of them.

This section starts with:
EXEC SQL BEGIN DECLARE SECTION;
And ends with:
EXEC SQL END DECLARE SECTION;
Between them, there must be regular C variable declarations. The following is an example.
int   x = 4;
char  foo[16], bar[16];
  • The type of the host variables declared between the start and end of the marked section must be one of the supported data types. For details, see Table 1.
  • You can also declare variables with the following syntax which implicitly creates a DECLARE section: EXEC SQL int i = 4.
  • Variables that are not intended to be used in SQL commands can be declared normally outside these special sections.
  • The definition of a structure or union also must be listed inside a DECLARE section. Otherwise, the preprocessor cannot process these types.