September 26, 2024: PostgreSQL 17 Released!
Supported Versions: Current (17) / 16 / 15 / 14 / 13 / 12
Development Versions: devel
Unsupported versions: 11 / 10 / 9.6 / 9.5 / 9.4 / 9.3 / 9.2 / 9.1 / 9.0 / 8.4 / 8.3 / 8.2
This documentation is for an unsupported version of PostgreSQL.
You may want to view the same page for the current version, or one of the other supported versions listed above instead.

32.12. Preprocessor directives

32.12.1. Including files

To include an external file into your embedded SQL program, use:

EXEC SQL INCLUDE filename;

The embedded SQL preprocessor will look for a file named filename.h, preprocess it, and include it in the resulting C output. Thus, embedded SQL statements in the included file are handled correctly.

Note that this is not the same as:

#include <filename.h>

because this file would not be subject to SQL command preprocessing. Naturally, you can continue to use the C #include directive to include other header files.

Note: The include file name is case-sensitive, even though the rest of the EXEC SQL INCLUDE command follows the normal SQL case-sensitivity rules.

32.12.2. The #define and #undef directives

Similar to the directive #define that is known from C, embedded SQL has a similar concept:

EXEC SQL DEFINE name;
EXEC SQL DEFINE name value;

So you can define a name:

EXEC SQL DEFINE HAVE_FEATURE;

And you can also define constants:

EXEC SQL DEFINE MYNUMBER 12;
EXEC SQL DEFINE MYSTRING 'abc';

Use undef to remove a previous definition:

EXEC SQL UNDEF MYNUMBER;

Of course you can continue to use the C versions #define and #undef in your embedded SQL program. The difference is where your defined values get evaluated. If you use EXEC SQL DEFINE then the ecpg preprocessor evaluates the defines and substitutes the values. For example if you write:

EXEC SQL DEFINE MYNUMBER 12;
...
EXEC SQL UPDATE Tbl SET col = MYNUMBER;

then ecpg will already do the substitution and your C compiler will never see any name or identifier MYNUMBER. Note that you cannot use #define for a constant that you are going to use in an embedded SQL query because in this case the embedded SQL precompiler is not able to see this declaration.

32.12.3. ifdef, ifndef, else, elif and endif directives

You can use the following directives to compile code sections conditionally:

EXEC SQL ifdef name;

Checks a name and processes subsequent lines if name has been created with EXEC SQL define name.

EXEC SQL ifndef name;

Checks a name and processes subsequent lines if name has not been created with EXEC SQL define name.

EXEC SQL else;

Starts processing an alternative section to a section introduced by either EXEC SQL ifdef name or EXEC SQL ifndef name.

EXEC SQL elif name;

Checks name and starts an alternative section if name has been created with EXEC SQL define name.

EXEC SQL endif;

Ends an alternative section.

Example:

   exec sql ifndef TZVAR;
   exec sql SET TIMEZONE TO 'GMT';
   exec sql elif TZNAME;
   exec sql SET TIMEZONE TO TZNAME;
   exec sql else;
   exec sql SET TIMEZONE TO TZVAR;
   exec sql endif;