IBM Books

Embedded SQL Programming Guide


Programming Considerations

Special host-language programming considerations are discussed in the following pages. Included is information on language restrictions, host language specific include files, embedding SQL statements, host variables, and supported data types for host variables.

Language Restrictions

All API pointers are 4 bytes long. All integer variables used as value parameters in API calls must be declared with a USAGE COMP-5 clause.

Input and Output Files

By default, the input file has an extension of .sqb, but if you use the TARGET precompile option (TARGET ANSI_COBOL, TARGET IBMCOB, TARGET MFCOB or TARGET MFCOB16), the input file can have any extension you prefer.

By default, the output file has an extension of .cbl, but you can use the OUTPUT precompile option to specify a new name and path for the output modified source file.

Include Files

The host-language-specific include files for COBOL have the file extension .cbl. The include files that are intended to be used in your applications are described below.

SQL (sql.cbl)
This file includes language-specific prototypes for the binder, precompiler, and error message retrieval APIs. It also defines system constants.

SQLAPREP (sqlaprep.cbl)
This file contains definitions required to write your own precompiler.

SQLCA (sqlca.cbl)
This file defines the SQL Communication Area (SQLCA) structure. The SQLCA contains variables that are used by the database manager to provide an application with error information about the execution of SQL statements and API calls.

SQLCA_92 (sqlca_92.cbl)
This file contains a FIPS SQL92 Entry Level compliant version of the SQL Communications Area (SQLCA) structure. This file should be included in place of the sqlca.cbl file when writing DB2 applications that conform to the FIPS SQL92 Entry Level standard. The sqlca_92.cbl file is automatically included by the DB2 precompiler when the LANGLEVEL precompiler option is set to SQL92E.

SQLCODES (sqlcodes.cbl)
This file defines constants for the SQLCODE field of the SQLCA structure.

SQLDA (sqlda.cbl)
This file defines the SQL Descriptor Area (SQLDA) structure. The SQLDA is used to pass data between an application and the database manager.

SQLEAU (sqleau.cbl)
This file contains constant and structure definitions required for the DB2 security audit APIs. If you use these APIs, you need to include this file in your program. This file also contains constant and keyword value definitions for fields in the audit trail record. These definitions can be used by external or vendor audit trail extract programs.

SQLENV (sqlenv.cbl)
This file defines language-specific calls for the database environment APIs, and the structures, constants, and return codes for those interfaces.

SQLETSD (sqletsd.cbl)
This file defines the Table Space Descriptor structure, SQLETSDESC, which is passed to the Create Database API, sqlgcrea.

SQLE819A (sqle819a.cbl)
If the code page of the database is 819 (ISO Latin-1), this sequence sorts character strings that are not FOR BIT DATA according to the host CCSID 500 (EBCDIC International) binary collation. This file is used by the CREATE DATABASE API.

SQLE819B (sqle819b.cbl)
If the code page of the database is 819 (ISO Latin-1), this sequence sorts character strings that are not FOR BIT DATA according to the host CCSID 037 (EBCDIC US English) binary collation. This file is used by the CREATE DATABASE API.

SQLE850A (sqle850a.cbl)
If the code page of the database is 850 (ASCII Latin-1), this sequence sorts character strings that are not FOR BIT DATA according to the host CCSID 500 (EBCDIC International) binary collation. This file is used by the CREATE DATABASE API.

SQLE850B (sqle850b.cbl)
If the code page of the database is 850 (ASCII Latin-1), this sequence sorts character strings that are not FOR BIT DATA according to the host CCSID 037 (EBCDIC US English) binary collation. This file is used by the CREATE DATABASE API.

SQLE932A (sqle932a.cbl)
If the code page of the database is 932 (ASCII Japanese), this sequence sorts character strings that are not FOR BIT DATA according to the host CCSID 5035 (EBCDIC Japanese) binary collation. This file is used by the CREATE DATABASE API.

SQLE932B (sqle932b.cbl)
If the code page of the database is 932 (ASCII Japanese), this sequence sorts character strings that are not FOR BIT DATA according to the host CCSID 5026 (EBCDIC Japanese) binary collation. This file is used by the CREATE DATABASE API.

SQL1252A (sql1252a.cbl)
If the code page of the database is 1252 (Windows Latin-1), this sequence sorts character strings that are not FOR BIT DATA according to the host CCSID 500 (EBCDIC International) binary collation. This file is used by the CREATE DATABASE API.

SQL1252B (sql1252b.cbl)
If the code page of the database is 1252 (Windows Latin-1), this sequence sorts character strings that are not FOR BIT DATA according to the host CCSID 037 (EBCDIC US English) binary collation. This file is used by the CREATE DATABASE API.

SQLMON (sqlmon.cbl)
This file defines language-specific calls for the database system monitor APIs, and the structures, constants, and return codes for those interfaces.

SQLMONCT (sqlmonct.cbl)
This file contains constant definitions and local data structure definitions required to call the Database System Monitor APIs.

SQLSTATE (sqlstate.cbl)
This file defines constants for the SQLSTATE field of the SQLCA structure.

SQLUTBCQ (sqlutbcq.cbl)
This file defines the Table Space Container Query data structure, SQLB-TBSCONTQRY-DATA, which is used with the table space container query APIs, sqlgstsc, sqlgftcq and sqlgtcq.

SQLUTBSQ (sqlutbsq.cbl)
This file defines the Table Space Query data structure, SQLB-TBSQRY-DATA, which is used with the table space query APIs, sqlgstsq, sqlgftsq and sqlgtsq.

SQLUTIL (sqlutil.cbl)
This file defines the language-specific calls for the utility APIs, and the structures, constants, and codes required for those interfaces.

Embedding SQL Statements

Embedded SQL statements consist of the following three elements:

Element
Correct COBOL Syntax
Keyword pair
EXEC SQL
Statement string
Any valid SQL statement
Statement terminator
END-EXEC.

For example:

     EXEC SQL SELECT col INTO :hostvar FROM table END-EXEC.

The following rules apply to embedded SQL statements:

Host Variables

Host variables are COBOL language variables that are referenced within SQL statements. They allow an application to pass input data to the database manager and receive output data from the database manager. After the application is precompiled, host variables are used by the compiler as any other COBOL variable. Obey the rules described below when naming, declaring, and using host variables.

Naming Host Variables

The SQL precompiler identifies host variables by their declared name. The following rules apply:

Declaring Host Variables

An SQL declare section must be used to identify host variable declarations. This alerts the precompiler to any host variables that can be referenced in subsequent SQL statements.

The COBOL precompiler only recognizes a subset of valid COBOL declarations.

Figure 37 shows the syntax for numeric host variables.

Figure 37. Syntax for Numeric Host Variables in COBOL

>>--.-01-.--variable-name--.-PICTURE-.-------------->
    '-77-'                 '-PIC-----'               
                                                     
   .-IS-.                                            
>--'----'--picture-string--------------------------->
                                                     
>--.----------------------------------------.------->
   |                             (1)        |        
   '-.---------------.--.-COMP-3----------.-'        
     |        .-IS-. |  |-COMPUTATIONAL-3-|          
     '-USAGE--'----'-'  |-COMP-5----------|          
                        '-COMPUTATIONAL-5-'          
                                                     
>--.----------------------.--.--><                   
   |        .-IS-.        |                          
   '-VALUE--'----'--value-'                          
                                                     
NOTE:                                                
(1)  An alternative for COMP-3 is PACKED-DECIMAL.    


Floating Point

>>--.-01-.--variable-name--.---------------.--------> 
    '-77-'                 |        .-IS-. |          
                           '-USAGE--'----'-'          
                                                      
                         (1)                          
>--.-.-COMPUTATIONAL-1-.-----.----------------------> 
   | '-COMP-1----------'     |                        
   |                     (2) |                        
   '-.-COMPUTATIONAL-2-.-----'                        
     '-COMP-2----------'                              
                                                      
>--.----------------------.--.--------------------->< 
   |        .-IS-.        |                           
   '-VALUE--'----'--value-'                           
                                                      
NOTES:                                                
(1)  REAL (SQLTYPE 480), Length 4                     
(2)  DOUBLE (SQLTYPE 480), Length 8                   

Notes:

  1. Picture-string must have one of the following forms:

  2. Nines may be expanded (e.g., "S999" instead of S9(3)")

  3. m and n must be positive integers.

Figure 38 shows the syntax for character host variables.

Figure 38. Syntax for Character Host Variables in COBOL

Fixed Length

>>--.-01-.--variable-name--.-PICTURE-.--------------> 
    '-77-'                 '-PIC-----'                
                                                      
   .-IS-.                                             
>--'----'--picture-string---------------------------> 
                                                      
>--.----------------------.--.--><                    
   |        .-IS-.        |                           
   '-VALUE--'----'--value-'                           

Variable Length

>>--01--variable-name--.--><                         
                                                     
                                                     
                                   .-IS-.            
>>--49--identifier-1--.-PICTURE-.--'----'--S9(4)---->
                      '-PIC-----'                    
                                                     
>--.----------------------------------------.------->
   '-.---------------.--.-COMP-5----------.-'        
     |        .-IS-. |  '-COMPUTATIONAL-5-'          
     '-USAGE--'----'-'                               
                                                     
>--.----------------------.--.--><                   
   |        .-IS-.        |                          
   '-VALUE--'----'--value-'                          
                                                     
                                                     
>>--49--identifier-2--.-PICTURE-.------------------->
                      '-PIC-----'                    
                                                     
   .-IS-.                                            
>--'----'--picture-string--------------------------->
                                                     
>--.----------------------.--.--><                   
   |        .-IS-.        |                          
   '-VALUE--'----'--value-'                          

Notes:

  1. Picture-string must have the form X(m). Alternately, X's may be expanded (for example, "XXX" instead of "X(3)").

  2. m is from 1 to 254 for fixed-length strings.

  3. m is from 1 to 32 700 for variable-length strings.

  4. If m is greater than 4 000, the host variable will be treated as a LONG VARCHAR string, and its use may be restricted.

  5. Use X and 9 as the picture characters in any PICTURE clause. Other characters are not allowed.

  6. Variable-length strings consist of a length item and a value item. You can use acceptable COBOL names for the length item and the string item. However, refer to the variable-length string by the collective name in SQL statements.

  7. In a CONNECT statement, such as shown below, COBOL character string host variables dbname and userid will have any trailing blanks removed before processing:
         EXEC SQL CONNECT TO :dbname USER :userid USING :p-word 
         END-EXEC.
    

    However, because blanks can be significant in passwords, the p-word host variable should be declared as a VARCHAR data item, so that your application can explicitly indicate the significant password length for the CONNECT statement as follows:

         EXEC SQL BEGIN DECLARE SECTION END-EXEC. 
         01 dbname PIC X(8). 
         01 userid PIC X(8). 
         01 p-word. 
             49 L PIC S9(4) COMP-5. 
             49 D PIC X(18). 
         EXEC SQL END DECLARE SECTION END-EXEC. 
         PROCEDURE DIVISION. 
             MOVE "sample" TO dbname. 
             MOVE "userid" TO userid. 
             MOVE "password" TO D OF p-word. 
             MOVE 8          TO L of p-word. 
         EXEC SQL CONNECT TO :dbname USER :userid USING :p-word 
         END-EXEC.
    

Figure 39 shows the syntax for graphic host variables.

Figure 39. Syntax for Graphic Host Variables in COBOL

Fixed Length

>>--.-01-.--variable-name--.-PICTURE-.--------------> 
    '-77-'                 '-PIC-----'                
                                                      
   .-IS-.                                             
>--'----'--picture-string--USAGE--------------------> 
                                                      
   .-IS-.                                             
>--'----'--DISPLAY-1--.----------------------.------> 
                      |        .-IS-.        |        
                      '-VALUE--'----'--value-'        

Variable Length

>>--01--variable-name--.--><                         
                                                     
                                                     
                                   .-IS-.            
>>--49--identifier-1--.-PICTURE-.--'----'--S9(4)---->
                      '-PIC-----'                    
                                                     
>--.----------------------------------------.------->
   '-.---------------.--.-COMP-5----------.-'        
     |        .-IS-. |  '-COMPUTATIONAL-5-'          
     '-USAGE--'----'-'                               
                                                     
>--.----------------------.--.--><                   
   |        .-IS-.        |                          
   '-VALUE--'----'--value-'                          
                                                     
                                                     
>>--49--identifier-2--.-PICTURE-.------------------->
                      '-PIC-----'                    
                                                     
   .-IS-.                                            
>--'----'--picture-string--USAGE-------------------->
                                                     
   .-IS-.                                            
>--'----'--DISPLAY-1--.----------------------.------>
                      |        .-IS-.        |       
                      '-VALUE--'----'--value-'       
 

Notes:

  1. Picture-string must have the form G(m). Alternately, G's may be expanded (for example, "GGG" instead of "G(3)").

  2. m is from 1 to 127 for fixed-length strings.

  3. m is from 1 to 16 350 for variable-length strings.

  4. If m is greater than 2 000, the host variable will be treated as a LONG VARGRAPHIC string, and its use may be restricted.

Indicator Variables

Indicator variables should be declared as a PIC S9(4) COMP-5 data type.

LOB Declarations in COBOL

Figure 40 shows the syntax for declaring large object (LOB) host variables in COBOL.

Figure 40. Syntax for LOB Host Variables in COBOL

>>--01--variable-name--.---------------.------------> 
                       '-USAGE-.----.--'              
                               '-IS-'                 
                                                      
>--SQL TYPE IS--.-BLOB---.--------------------------> 
                |-CLOB---|                            
                '-DBCLOB-'                            
                                                      
>--(--length--.---.--)--.-------------------------->< 
              |-K-|                                   
              |-M-|                                   
              '-G-'                                   

Notes:

  1. For BLOB and CLOB  1 <= lob-length <= 2 147 483 647.

  2. For DBCLOB  1 <= lob-length <= 1 073 741 823.

  3. SQL TYPE IS, BLOB, CLOB, DBCLOB, K, M, G can be in either uppercase, lowercase, or mixed.

  4. Initialization within the LOB declaration is not permitted.

  5. The host variable name prefixes LENGTH and DATA in the precompiler generated code.

BLOB Example:

Declaring:

     01 MY-BLOB USAGE IS SQL TYPE IS BLOB(2M). 

Results in the generation of the following structure:

     01 MY-BLOB. 
        49 MY-BLOB-LENGTH PIC S9(9) COMP-5. 
        49 MY-BLOB-DATA PIC X(2097152). 

CLOB Example:

Declaring:

     01 MY-CLOB USAGE IS SQL TYPE IS CLOB(125M). 

Results in the generation of the following structure:

     01 MY-CLOB. 
        49 MY-CLOB-LENGTH PIC S9(9) COMP-5. 
        49 MY-CLOB-DATA PIC X(131072000). 

DBCLOB Example:

Declaring:

          01 MY-DBCLOB USAGE IS SQL TYPE IS DBCLOB(30000). 

Results in the generation of the following structure:

     01 MY-DBCLOB. 
        49 MY-DBCLOB-LENGTH PIC S9(9) COMP-5. 
        49 MY-DBCLOB-DATA PIC G(30000) DISPLAY-1. 

LOB Locator Declarations in COBOL

Figure 41 shows the syntax for declaring large object (LOB) locator host variables in COBOL.

Figure 41. Syntax for LOB Locator Host Variables in COBOL

 >>--01--variable-name--.---------------.------------>
                        '-USAGE-.----.--'             
                                '-IS-'                
                                                      
 >--SQL TYPE IS--.-BLOB-LOCATOR---.--.--------------><
                 |-CLOB-LOCATOR---|                   
                 '-DBCLOB-LOCATOR-'                   

Notes:

  1. SQL TYPE IS, BLOB-LOCATOR, CLOB-LOCATOR, DBCLOB-LOCATOR can be either uppercase, lowercase, or mixed.

  2. Initialization of locators is not permitted.

BLOB Locator Example (other LOB locator types are similar):

Declaring:

     01 MY-LOCATOR USAGE SQL TYPE IS BLOB-LOCATOR. 

Results in the generation of the following declaration:

     01 MY-LOCATOR PIC S9(9) COMP-5. 

File Reference Declarations in COBOL

Figure 42 shows the syntax for declaring file reference host variables in COBOL.

Figure 42. Syntax for File Reference Host Variables in COBOL

>>--01--variable-name--.---------------.------------> 
                       '-USAGE-.----.--'              
                               '-IS-'                 
                                                      
>--SQL TYPE IS--.-BLOB-FILE---.--.----------------->< 
                |-CLOB-FILE---|                       
                '-DBCLOB-FILE-'                       

BLOB File Reference Example (other LOB types are similar):

Declaring:

     01 MY-FILE USAGE IS SQL TYPE IS BLOB-FILE. 

Results in the generation of the following declaration:

     01 MY-FILE. 
        49 MY-FILE-NAME-LENGTH PIC S9(9) COMP-5. 
        49 MY-FILE-DATA-LENGTH PIC S9(9) COMP-5. 
        49 MY-FILE-FILE-OPTIONS PIC S9(9) COMP-5. 
        49 MY-FILE-NAME PIC X(255). 

Host structure support in COBOL

The COBOL precompiler supports declarations of group data items in the host variable declare section. Among other things, this provides a shorthand for referring to a set of elementary data items in an SQL statement. For example, the following group data item can be used to access the STAFF table in the SAMPLE database:

       01 staff-record. 
           05 staff-id      pic s9(4) comp-5. 
           05 staff-name. 
               49 l         pic s9(4) comp-5. 
               49 d         pic x(9). 
           05 staff-dept    pic s9(4) comp-5. 
           05 staff-job     pic x(5). 
           05 staff-years   pic s9(4) comp-5. 
           05 staff-salary  pic s9(5)v99 comp-3. 
           05 staff-comm    pic s9(5)v99 comp-3. 

Group data items in the declare section can have any of the valid host variable types described above as subordinate data items. This includes all numeric and character types, as well as all large object types. Group data items cannot be nested. The exception is the varchar character type, shown in staff-name above. It is permitted within a group data item because the precompiler treats it as a single host variable instead of a group. Note that you must declare varchar character types with the subordinate items at level 49, as in the above example. If they are not at level 49, the varchar is treated as a group data item with two subordinates, and is subject to the rules of declaring and using group data items. You must declare group data items at the 01-level, and the subordinate items within them at a common level between 02 and 49. For example, all the subordinates in staff-record above are at the 05 level. FILLER items are not permitted within group data items.

You can use group data items and their subordinates in three ways:

Other uses of group items which cause an SQL0087N to occur include PREPARE, EXECUTE IMMEDIATE, CALL, indicator variables, and SQLDA references. Groups with only one subordinate are permitted in such situations, as are references to individual subordinates, as in method 2 and 3 above.

Indicator Tables

The COBOL precompiler supports the declaration of tables of indicator variables, which are convenient to use with group data items. They are declared as follows:

     01 <indicator-table-name>. 
         05 <indicator-name> pic s9(4) comp-5 
                             occurs <table-size> times. 

For example:

     01 staff-indicator-table. 
         05 staff-indicator pic s9(4) comp-5 
                            occurs 7 times. 

This indicator table can be used effectively with the first format of group item reference above:

      EXEC SQL SELECT ID, NAME, DEPT, JOB, YEARS, SALARY, COM 
        INTO :staff-record :staff-indicator 
        FROM STAFF WHERE ID = 10 END-EXEC. 

Here, the precompiler detects that staff-indicator was declared as an indicator table, and expands it into individual indicator references when it processes the SQL statement. staff-indicator(1) is associated with staff-id of staff-record, staff-indicator(2) is associated with staff-name of staff-record, and so on.
Note:If there are k more indicator entries in the indicator table than there are subordinates in the data item, (for example, if staff-indicator has 10 entries, making k=3), the k extra entries at the end of the indicator table are ignored. Likewise, if there are k fewer indicator entries than subordinates, the last k subordinates in the group item do not have indicators associated with them. Note that you cannot individually refer to elements in an indicator table in an SQL statement.

Using BINARY/COMP-4 COBOL Data Types

The DB2 COBOL precompiler supports the use of BINARY, COMP, and COMP-4 data types wherever integer host variables and indicators are permitted, as long as the target COBOL compiler views (or can be made to view) the BINARY, COMP, or COMP-4 data types as equivalent to the COMP-5 data type. In this book, such host variables and indicators are shown with type COMP-5. Target compilers supported by DB2 that treat COMP, COMP-4, BINARY COMP and COMP-5 as equivalent are:

Using REDEFINES in COBOL Group Data Items

You can use the REDEFINES clause when declaring host variables. If you declare a member of a group data item with the REDEFINES clause and that group data item is referred to as a whole in an SQL statement, any subordinate items containing the REDEFINES clause are not expanded. For example:

   01 foo.                        
    10 a pic s9(4) comp-5.     
    10 a1 redefines a pic x(2).
    10 b pic x(10).            

Referring to foo in an SQL statement as follows:

    ... INTO :foo ...                                              

The above statement is equivalent to:

    ... INTO :foo.a, :foo.b ...                                    

That is, the subordinate item a1, declared with the REDEFINES clause is not automatically expanded out in such situations. If a1 is unambiguous, you can explicitly refer to a subordinate with a REDEFINES clause in an SQL statement, as follows:

    ... INTO :foo.a1 ...                                           

or

    ... INTO :a1 ...                                               

Supported SQL Data Types

Certain predefined COBOL data types correspond to column types. Only these COBOL data types can be declared as host variables.

Table 17 shows the COBOL equivalent of each column type. When the precompiler finds a host variable declaration, it determines the appropriate SQL type value. The database manager uses this value to convert the data exchanged between the application and itself.

Not every possible data description for host variables is recognized. COBOL data items must be consistent with the ones described in the following table. If you use other data items, an error can result.

Table 17. SQL Data Types Mapped to COBOL Declarations
SQL Column Type1 COBOL Data Type SQL Column Type Description
 
SMALLINT 
(500 or 501) 

01 name PIC S9(4) COMP-5. 16-bit signed integer
INTEGER 
(496 or 497) 

01 name PIC S9(9) COMP-5. 32-bit signed integer
 
DECIMAL(p,s) 
(484 or 485) 

01 name PIC S9(m) V9(n) COMP-3. Packed decimal
 
REAL2
(480 or 481) 

01 name USAGE IS COMP-1. Single-precision floating point
 
DOUBLE3
(480 or 481) 

01 name USAGE IS COMP-2. Double-precision floating point
 
CHAR(n) 
(452 or 453) 

01 name PIC X(n). Fixed-length character string
 
VARCHAR(n) 
(448 or 449) 

 
01 name. 
 49 length PIC S9(4) COMP-5. 
 49 name PIC X(n). 
  
1<=n<=4000 

Variable-length character string
 
LONG VARCHAR 
(456 or 457) 

 
01 name. 
 49 length PIC S9(4) COMP-5. 
 49 data PIC X(n). 
  
4001<=n<=32 700 

Long variable-length character string
 
CLOB(n) 
(408 or 409) 

 
01 MY-CLOB USAGE IS SQL TYPE IS CLOB(n). 
  
1<=n<=2 147 483 647 

Large object variable-length character string
 
CLOB locator variable4
(964 or 965) 

01 MY-CLOB-LOCATOR USAGE IS SQL TYPE IS CLOB-LOCATOR. Identifies CLOB entities residing on the server
 
CLOB file reference variable4
(808 or 809) 

01 MY-CLOB-FILE USAGE IS SQL TYPE IS CLOB-FILE. Descriptor for file containing CLOB data
 
BLOB(n) 
(404 or 405) 

01 MY-BLOB USAGE IS SQL TYPE IS BLOB(n).
 
  
1<=n<=2 147 483 647 
Large object variable-length binary string
 
BLOB locator variable4
(960 or 961) 

01 MY-BLOB-LOCATOR USAGE IS SQL TYPE IS BLOB-LOCATOR. Identifies BLOB entities residing on the server
 
BLOB file reference variable4
(804 or 805) 

01 MY-CLOB-FILE USAGE IS SQL TYPE IS CLOB-FILE. Descriptor for file containing CLOB data
 
DATE 
(384 or 385) 

01 identifier PIC X(10). 10-byte character string
 
TIME 
(388 or 389) 

01 identifier PIC X(8). 8-byte character string
 
TIMESTAMP 
(392 or 393) 

01 identifier PIC X(26). 26-byte character string
Note:The following data types are only available in the DBCS environment.
 
GRAPHIC(n) 
(468 or 469) 

01 name PIC G(n) DISPLAY-1. Fixed-length double-byte character string
 
VARGRAPHIC(n) 
(464 or 465) 

 
01 name. 
 49 length PIC S9(4) COMP-5. 
 49 name PIC G(n) DISPLAY-1. 
  
1<=n<=2 000

Variable length double-byte character string with 2-byte string length indicator
 
LONG VARGRAPHIC 
(472 or 473) 

01 name. 
 49 length PIC S9(4) COMP-5. 
 49 name PIC G(n) DISPLAY-1. 
  
2001<=n<=16 350

Variable length double-byte character string with 2-byte string length indicator
 
DBCLOB(n) 
(412 or 413) 

01 MY-DBCLOB USAGE IS SQL TYPE IS DBCLOB(n).
 
  
1<=n<=1 073 741 823 
Large object variable length double-byte character string with 4-byte string length indicator
 
DBCLOB locator variable4
(968 or 969) 

01 MY-DBCLOB-LOCATOR USAGE IS SQL TYPE IS DBCLOB-LOCATOR. Identifies DBCLOB entities residing on the server
 
DBCLOB file reference variable4
(812 or 813) 

01 MY-DBCLOB-FILE USAGE IS SQL TYPE IS DBCLOB-FILE. Descriptor for file containing DBCLOB data
Notes:
  1. The first number under Column Type indicates that an indicator variable is not provided, and the second number indicates that an indicator variable is provided. An indicator variable is needed to indicate NULL values, or to hold the length of a truncated string. These are the values that would appear in the SQLTYPE field of the SQLDA for these data types.
  2. FLOAT(n) where 0 < n < 25 is a synonym for REAL. The difference between REAL and DOUBLE in the SQLDA is the length value (4 or 8).
  3. The following SQL types are synonyms for DOUBLE:
    • FLOAT
    • FLOAT(n) where 24 < n < 54 is
    • DOUBLE PRECISION
  4. This is not a column type but a host variable type.

The following is a sample SQL declare section with a host variable declared for each supported SQL data type.

     EXEC SQL BEGIN DECLARE SECTION END-EXEC. 
     * 
       01 age        PIC S9(4) COMP-5. 
       01 divis      PIC S9(9) COMP-5. 
       01 salary     PIC S9(6)V9(3) COMP-3.
       01 bonus      USAGE IS COMP-1. 
       01 wage       USAGE IS COMP-2. 
       01 nm         PIC X(5). 
       01 varchar. 
          49 leng    PIC S9(4) COMP-5. 
          49 strg    PIC X(14). 
       01 longvchar. 
          49 len     PIC S9(4) COMP-5. 
          49 str     PIC X(6027). 
       01 MY-CLOB USAGE IS SQL TYPE IS CLOB(1M). 
       01 MY-CLOB-LOCATOR USAGE IS SQL TYPE IS CLOB-LOCATOR. 
       01 MY-CLOB-FILE USAGE IS SQL TYPE IS CLOB-FILE. 
       01 MY-BLOB USAGE IS SQL TYPE IS BLOB(1M). 
       01 MY-BLOB-LOCATOR USAGE IS SQL TYPE IS BLOB-LOCATOR. 
       01 MY-BLOB-FILE USAGE IS SQL TYPE IS BLOB-FILE. 
       01 MY-DBCLOB USAGE IS SQL TYPE IS DBCLOB(1M). 
       01 MY-DBCLOB-LOCATOR USAGE IS SQL TYPE IS DBCLOB-LOCATOR. 
       01 MY-DBCLOB-FILE USAGE IS SQL TYPE IS DBCLOB-FILE. 
       01 MY-PICTURE PIC G(16000) USAGE IS DISPLAY-1. 
       01 dt         PIC X(10). 
       01 tm         PIC X(8). 
       01 tmstmp     PIC X(26). 
       01 wage-ind   PIC S9(4) COMP-5. 
     * 
     EXEC SQL END DECLARE SECTION END-EXEC. 

The following are additional rules for supported COBOL data types:

FOR BIT DATA

Certain database columns can be declared FOR BIT DATA. These columns, which generally contain characters, are used to hold binary information. The CHAR(n), VARCHAR, LONG VARCHAR, and BLOB data types are the COBOL host variable types that can contain binary data. Use these data types when working with columns with the FOR BIT DATA attribute.

SQLSTATE and SQLCODE Variables

When using the LANGLEVEL precompile option with a value of SQL92E, the following two declarations may be included as host variables:

  EXEC SQL BEGIN DECLARE SECTION END-EXEC.
  01 SQLSTATE PICTURE X(5). 
  01 SQLCODE  PICTURE S9(9) USAGE COMP. 
  .
  .
  .
  EXEC SQL END DECLARE SECTION END-EXEC.

If neither of these is specified, the SQLCODE declaration is assumed during the precompile step. The '01' can also be '77' and the 'PICTURE' can be 'PIC'. Note that when using this option, the INCLUDE SQLCA statement should not be specified.

For applications made up of multiple source files, the SQLCODE and SQLSTATE declarations may be included in each source file as shown above.

Japanese or Traditional-Chinese EUC Considerations

Any graphic data sent from your application running under an EUC code set is tagged with the UCS-2 code page identifier, so your application must convert an EUC string to UCS-2 before sending it to a database server. Likewise, graphic data retrieved from a database by your application running under an EUC code is encoded using UCS-2. This requires your application to convert from UCS-2 to EUC internally, unless the user is to be presented with UCS-2 data. You are responsible for converting to and from UCS-2 since this conversion must be conducted before the data is copied to, and after it is copied from, the SQLDA. No application-accessible conversion routines are supplied with the DB2 Universal Database products. Use system calls available from your operating system.

For general EUC application development guidelines, see "Japanese and Traditional-Chinese EUC Code Set Considerations".

Object Oriented COBOL

If you are using Object Oriented COBOL, you must observe the following:


[ Top of Page | Previous Page | Next Page | Table of Contents | Index ]

[ DB2 List of Books | Search the DB2 Books ]