3. Embedded SQL for COBOL : COBOL Data Items and Data Types : Data Types : DCLGEN Utility
 
Share this page                  
DCLGEN Utility
DCLGEN (Declaration Generator) is a structuregenerating utility that maps the columns of a database table into a structure (a COBOL record) that can be included in an embedded SQL declaration section.
The DCLGEN utility can be invoked from the operating system level by executing the following command:
dclgen language dbname tablename filename structurename
language
Defines the embedded SQL host language, in this case, COBOL.
dbname
Defines the name of the database containing the table.
tablename
Defines the name of the database table.
filename
Defines the output file into which the structure declaration is placed.
structurename
Defines the name of the host language structure (COBOL record) that the command generates.
This command creates the declaration file filename, containing a record corresponding to the database table. The file also includes a declare table statement that serves as a comment and identifies the database table and columns from which the record was generated.
After the file has been generated, you can use an embedded SQL include statement to incorporate it into the embedded SQL variable declaration section. The following example demonstrates how to use DCLGEN in a COBOL program.
Assume the Employee table was created in the Personnel database as:
EXEC SQL CREATE TABLE employee
(eno integer NOT NULL,
ename char(20) NOT NULL,
age integer1,
job smallint,
sal decimal (14,2) NOT NULL,
dept smallint,
vacation float,
resume long varchar)
END-EXEC.
and the DCLGEN systemlevel command is:
DCLGEN cobol personnel employee employee.dcl emprec
The employee.dcl file created by this command 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 COBOL emprec record.
Windows and UNIX:
The contents of the employee.dcl file are:
* Description of table "employee" from database * "personnel"
EXEC SQL DECLARE employee TABLE
(eno integer NOT NULL,
 ename char(20) NOT NULL,
 age integer1,
 job smallint,
 sal decimal(14,2) NOT NULL,
 dept smallint
 vacation float,
 resume long varchar)
END-EXEC.
01 EMPREC.
 02 ENO         PIC S9(9) USAGE COMP.
 02 ENAME       PIC X(20).
 02 AGE         PIC S9(5) USAGE COMP.
 02 JOB         PIC S9(5) USAGE COMP.
 02 SAL         PIC S9(12)V9(2) USAGE COMP-3.
 02 DEPT        PIC S9(5) USAGE COMP.
 02 VACATION    PIC S9(10)V9(8) USAGE COMP-3.
 02 RESUME      PIC X(0).
VMS:
The contents of the employee.dcl file are:
* Description of table "employee" from database * "personnel"
EXEC SQL DECLARE employee TABLE
(eno        integer NOT NULL,
ename      char(20) NOT NULL,
age        integer1,
job        smallint,
sal        decimal (14,2) NOT NULL,
dept       smallint
vacation   float,
resume     long varchar)
END-EXEC.
01 EMPREC.
02 ENO       PIC S9(9) USAGE COMP.
02 ENAME     PIC X(20).
02 AGE       PIC S9(5) USAGE COMP.
02 JOB       PIC S9(4) USAGE COMP.
02 SAL       PIC S9(12)V9(2) USAGE COMP-3.
02 DEPT      PIC S9(4) USAGE COMP.
02 VACATION  USAGE COMP-2.
02 RESUME    PIC X(0).
Use the embedded SQL include statement, in an embedded SQL declaration section, to include this file as follows:
EXEC SQL BEGIN DECLARE SECTION END-EXEC.
    EXEC SQL INCLUDE 'employee.dcl' END-EXEC.
EXEC SQL END DECLARE SECTION END-EXEC.
You can then use the emprec record in a select, fetch, or insert statement.
UNIX:
The default generated picture string for Ingres floating-point data is S9(10)V9(8).
DCLGEN converts underscores in column names to dashes when it generates names of the elements of the COBOL record. For example, a column name of column_1 translates to a record element name of column-1. Column names that begin or end with an underscore thus generate record element names unacceptable to the COBOL compiler.
Since COBOL supports packed decimal data, the structure member's type will be packed decimal with a precision and scale that matches the scale and precision of the database column.
Both VMS and Micro Focus COBOL only allow a maximum precision of 18, otherwise a compiler error is generated. Ingres allows 39 precision. If the decimal column is greater than 18, DCLGEN displays a warning message and generates a COBOL variable of S9(10)V9(8). You must verify that this is an acceptable size for the decimal columns because if it's not, you must manually modify the DCLGEN output file.
The field names of 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.
DCLGEN and Large Objects
When a table contains a large object column, DCLGEN will issue a warning message and map the column to a zero length character string variable. You must modify the length of the generated variable before attempting to use the variable in an application.
For example, assume that the job_description table was created in the personnel database as:
create table job_description
          (job          smallint,
           description long varchar));
and the DCLGEN system-level command is:
dclgen cobol personnel job_description jobs.dcl jobs_rec
The contents of the jobs.dcl file would be:
* Description of the table "employee" from database "personnel"
    EXEC SQL DECLARE long_obj_table TABLE
                  (job          smallint,
                   description long varchar));
 01 JOBS_REC.
    02 JOB               PICTURE S9(4) USAGE COMP.
    02 DESCRIPTION       PICTURE X(0).