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

Directives define and undef

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;
EXEC SQL UNDEF name;
The following is an example.
/* Define a name. */
EXEC SQL DEFINE HAVE_FEATURE;

/* Define constants. */
EXEC SQL DEFINE MYNUMBER 12;
EXEC SQL DEFINE MYSTRING 'abc';

/* Use undef to remove a previous definition. */
EXEC SQL UNDEF MYNUMBER;
You can also 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 ECPG evaluates the definitions and substitutes the values. In the following example, ECPG does the substitution and the compiler will never see any name or identifier MYNUMBER.
EXEC SQL DEFINE MYNUMBER 12; 
... 
EXEC SQL UPDATE Tbl SET col = MYNUMBER;

You cannot use #define for a variable used in an embedded SQL query because the embedded SQL precompiler cannot execute the declaration.