3. Embedded SQL for COBOL : COBOL Data Items and Data Types : Data Types : Embedded SQL/COBOL Declarations Example
 
Share this page                  
Embedded SQL/COBOL Declarations Example
The following UNIX, Windows, and VMS examples demonstrate some simple embedded SQL/COBOL declarations.
Windows:
EXEC SQL INCLUDE SQLCA END-EXEC.
EXEC SQL BEGIN DECLARE SECTION END-EXEC.
UNIX:
EXEC SQL INCLUDE SQLCA END-EXEC.
EXEC SQL BEGIN DECLARE SECTION END-EXEC.
* Data item to hold database name.
 01 DBNAME PIC X(9) VALUE IS "Personnel".

* Scaled data
 01 SALARY PIC S9(8)V9(2) USAGE COMP.
 01 MONEY PIC S999V99 USAGE COMP-3.

* Array of numerics
 01 NUMS.
   02 NUM-ARR PIC S99 OCCURS 10 TIMES.

* Record of a full name and a redefinition of its parts.
 01 NAME-REC.
   02 FULL-NAME PIC X(20).
   02 NAME-PARTS REDEFINES FULL-NAME.
      03 FIRST-NAME PIC X(8).
      03 MIDDLE-INIT PIC X(2).
      03 LAST-NAME PIC X(10).

* Record for fetching and displaying.
 01 OUT-REC.
   02 FILLER PIC X(15) VALUE "Value fetched: ".
   02 FROM-DB PIC S9(4) USAGE DISPLAY.

* Miscellaneous attributes (ignored by preprocessor).
 01 SALES-TOT PIC S9(6)V99 SIGN IS TRAILING.
 01 SYNC-REC.
   02 NUM1 PIC S99 USAGE COMP SYNCHRONIZED.
   02 FILLER PIC XX VALUE SPACES.
   02 NUM2 PIC S99 USAGE COMP SYNCHRONIZED.
01 RIGHT-ALIGN PIC X(30) JUSTIFIED RIGHT.
01 NUM-OUT PIC S99V99 USAGE DISPLAY BLANK WHEN ZERO.
EXEC SQL END DECLARE SECTION END-EXEC.
VMS:
EXEC SQL INCLUDE SQLCA END-EXEC.
EXEC SQL BEGIN DECLARE SECTION END-EXEC.
* Data item to hold database name.
 01 DBNAME PIC X(9) VALUE IS "Personnel".

* Scaled data
 01 SALARY USAGE COMP-1.
 01 MONEY PIC S999V99 USAGE COMP-3.

* Array of numerics
 01 NUMS.
   02 NUM-ARR  PIC S99 OCCURS 10 TIMES.

* Record of a full name and a redefinition of its parts.
 01 NAME-REC.
   02 FULL-NAME PIC X(20).
   02 NAME-PARTS REDEFINES FULL-NAME.
      03 FIRST-NAME PIC X(8).
      03 MIDDLE-INIT PIC X(2).
      03 LAST-NAME PIC X(10).

* Record for fetching and displaying.
 01 OUT-REC.
   02 FILLER PIC X(15) VALUE "Value fetched: ".
   02 FROM-DB PIC S9(4) USAGE DISPLAY.

* Miscellaneous attributes (ignored by preprocessor).
 01 SALES-TOT PIC S9(6)V99 SIGN IS TRAILING.
 01 SYNC-REC.
   02 NUM1 PIC S99 USAGE COMP SYNCHRONIZED.
   02 FILLER PIC XX VALUE SPACES.
   02 NUM2 USAGE COMP-2 SYNCHRONIZED.
 01 RIGHT-ALIGN PIC X(30) JUSTIFIED RIGHT.
 01 NUM-OUT PIC S99V99 USAGE DISPLAY BLANK
            WHEN ZERO.
EXEC SQL END DECLARE SECTION END-EXEC.