select open change scope Open full search

PG.CENTER connects PostgreSQL documentation, reference, and ecosystem knowledge. Maintained by Pigsty.

Supported versions: Current (18) / 17 / 16 / 15 / 14
Development versions: 19 / devel
Unsupported versions: 13 / 12 / 11 / 10 / 9.6 / 9.5 / 9.4 / 9.3 / 9.2 / 9.1 / 9.0 / 8.4 / 8.3 / 8.2 / 8.1 / 8.0 / 7.4 / 7.3
Historical version. PostgreSQL 7.3 reached end of support in November 2007. See the current version for supported releases.

4.5. Passing Data

To pass data from the program to the database, for example as parameters in a query, 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 a specially marked section, so 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 those lines, there must be normal C variable declarations, such as

int   x;
char  foo[16], bar[16];

The declarations are also echoed to the output file as a normal C variables, so there's no need to declare them again. Variables that are not intended to be used with 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 handle these types since it does not know the definition.

The special types VARCHAR and VARCHAR2 are converted into a named struct for every variable. A declaration like:

VARCHAR var[180];

is converted into:

struct varchar_var { int len; char arr[180]; } var;

This structure is suitable for interfacing with SQL datums of type VARCHAR.

To use a properly declared C variable in an SQL statement, write :varname where an expression is expected. See the previous section for some examples.

Report a documentation issue

Read the upstream documentation. For corrections, first check the current version.