dclgen Example
The following example demonstrates how to use DCLGEN within a C program (assuming the Employee table was created in the Personnel database):
exec sql create table employee
(eno smallint not null,
ename char(20) not null,
age integer1,
job smallint,
sal decimal(14,2) not null,
dept smallint)
with journaling;
The DCLGEN system-level command is:
dclgen c personnel employee employee.dcl emprec
This command creates the employee.dcl file, which contains a comment and two statements. The first statement is the declare table description of employee, which serves as a comment. The second statement is a declaration of the C structure emprec.
The contents of the employee.dcl file are:
/* Table employee description from database personnel */
exec sql declare employee table
(eno smallint not null,
ename char(20) not null,
age integer1,
job smallint,
sal decimal(14,2) not null,
dept smallint);
struct emprec_ {
short eno;
char ename[21];
short age;
short job;
double sal;
short dept;
} emprec;
The length of the ename buffer is increased by one byte to accommodate the C null terminator. Also, the integer1 data type is mapped to short rather than char.
To include this file in an embedded SQL declaration section, use the embedded SQL include statement:
exec sql begin declare section;
exec sql include 'employee.dcl';
exec sql end declare section;
You can then use the emprec structure within a select, fetch, or insert statement.
The field names in the structure that DCLGEN generates are identical to the column names in the specified table. Therefore, if the column names in the table contain any characters that are illegal for host language variable names you must modify the name of the field before attempting to use the variable in an application.
Last modified date: 08/14/2024