Updated on 2024-05-07 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;
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;

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.