SQL Keywords

SQL SECTION

What is SQL SECTION in Embedded SQL?

Marks the start and end of a host-variable declaration block in embedded SQL.
Sign up to get up to date news on SQL keywords
Welcome to the Galaxy, Guardian!
You'll be receiving a confirmation email

Follow us on twitter :)
Oops! Something went wrong while submitting the form.

Compatible dialects for SQL SECTION: Oracle (Pro*C, Pro*COBOL), PostgreSQL (ecpg), IBM Db2, Informix ESQL/C, SAP ASE, Teradata Preprocessor2

SQL SECTION Full Explanation

SQL SECTION is part of the Embedded SQL standard (ISO/IEC 9075-2). It appears only inside EXEC SQL statements and is used to declare host variables that a host language (C, C++, COBOL, etc.) can pass to and from the database. The block is delimited by EXEC SQL BEGIN DECLARE SECTION; and EXEC SQL END DECLARE SECTION;. Everything between those two statements is treated by the pre-compiler as host-language code whose variables become usable in subsequent embedded SQL commands. The SECTION keyword itself does not execute at runtime; it guides the pre-compiler. Omitting it or mis-pairing BEGIN and END leads to compilation errors. SECTION is mandatory when you want to use host variables without explicitly prefixing each one with a host indicator (for example, :var in Pro*C).

SQL SECTION Syntax

EXEC SQL BEGIN DECLARE SECTION;
/* host-language variable declarations */
EXEC SQL END DECLARE SECTION;

SQL SECTION Parameters

Example Queries Using SQL SECTION

EXEC SQL BEGIN DECLARE SECTION;
    int   user_id;
    char  user_email[255];
EXEC SQL END DECLARE SECTION;

/* later in the program */
EXEC SQL SELECT email INTO :user_email FROM users WHERE id = :user_id;

Expected Output Using SQL SECTION

  • The pre-compiler registers user_id and user_email as host variables
  • At runtime the SELECT copies the email column into user_email

Use Cases with SQL SECTION

  • Declaring C variables that will hold query results
  • Passing COBOL variables into INSERT or UPDATE statements
  • Grouping multiple host-language declarations in one place for clarity
  • Ensuring portable embedded SQL across databases that implement ISO standards

Common Mistakes with SQL SECTION

  • Forgetting EXEC SQL keywords (BEGIN DECLARE SECTION alone is invalid)
  • Omitting the matching END DECLARE SECTION
  • Placing executable SQL inside the DECLARE SECTION block
  • Attempting to use SECTION outside an embedded SQL pre-compiler

Related Topics

EXEC SQL, Host Variables, PREPARE, DECLARE CURSOR, FETCH

First Introduced In

SQL-92 Embedded SQL (ISO/IEC 9075-2:1992)

Frequently Asked Questions

What languages can use SQL SECTION?

C, C++, COBOL, and other languages that have an Embedded SQL pre-compiler compatible with ISO/IEC 9075-2.

Can I nest DECLARE SECTION blocks?

Most pre-compilers do not allow nesting. Use one BEGIN/END pair per compilation unit.

Is SECTION required for every host variable?

If your pre-compiler supports the :host_var syntax outside SECTION blocks, you can omit it, but using SECTION improves readability and portability.

Does SQL SECTION affect runtime performance?

No. It is a compile-time directive only; it has zero runtime cost.

Sign up to get up to date news on SQL keywords
Welcome to the Galaxy, Guardian!
You'll be receiving a confirmation email

Follow us on twitter :)
Oops! Something went wrong while submitting the form.
Trusted by top engineers on high-velocity teams
Aryeo Logo
Assort Health
Curri
Rubie Logo
Bauhealth Logo
Truvideo Logo

Check out other commonly used SQL Keywords!