Using DB2 files

To access DB2® files from a COBOL application that runs under AIX®, you must follow guidelines for compiling and linking the application and for identifying the DB2 file system, the DB2 instance and database, and the DB2 files.

About this task

  1. Compile and link the COBOL programs in your application by using the cob2 command.
  2. Initialize the DB2 environment by executing the profile for the DB2 instance that you want to use.
    For example, you might issue the following command to use instance db2inst1:
    . /home/db2inst1/sqllib/db2profile
  3. Ensure that the DB2 instance is running and that you can connect to the database that you want to access.
    The following example shows the db2 command that you might use to connect to database db2cob, and shows the system response:
    > db2 connect to db2cob
            Database Connection Information
            Database server        = DB2/AIX64 9.7.0
            SQL authorization ID   = MYUID
            Local database alias   = DB2COB
    Restriction: Note that COBOL applications can access DB2 files in only one database and DB2 instance at a time.
  4. Set environment variable DB2DBDFT to the intended database. For example:
    export DB2DBDFT=db2cob
  5. For the files that use DB2, specify the DB2 file system (either as the value of the FILESYS runtime option or directly in the assignment-name value). Use the fully qualified DB2 table name, including the schema name.
    For example, the following command completes the assignment of a transaction file TRANFILE to system file-name TEST.TRANS in the DB2 file system:
    export TRANFILE=DB2-TEST.TRANS

Creating DB2 files:

You can create a DB2 file in any of several ways:
  • By using an OPEN statement in your COBOL program
  • By using the TXSeries cicsddt utility

    For more information about the cicsddt command, see the TXSeries Knowledge Center referenced below.

  • By using the db2 create command
For example, you might use the following command sequence to create a relative file called EXAMPLE under schema CICS:
db2 create table cics.example\
"(rba char(4) not null for bit data, f1 varchar(80) not null for bit data)"
db2 create unique index cics.example0 on cics.example\
"(rba) disallow reverse scans"
db2 create unique index cics.example0@ on cics.example\
"(rba desc) disallow reverse scans"
You can display the resulting table by issuing the db2 describe command. For example:
> db2 describe table cics.example

                         Data type                     Column
Column name              schema    Data type name      Length     Scale Nulls
------------------------ --------- ------------------- ---------- ----- ------
RBA                      SYSIBM    CHARACTER                    4     0 No
F1                       SYSIBM    VARCHAR                     80     0 No

  2 record(s) selected.

The file CICS.EXAMPLE has variable-length records that would be compatible with a COBOL FILE SECTION definition of minimum record length between 0 and 79.

For more information about the functions that are provided by the db2 utility, enter the command db2.

For information about the additional requirements that apply to using DB2 files with CICS® TXSeries, see the related reference about the DB2 file system.