Module 14:- Database Interface
COBOL – Database Interface
- COBOL program can interface with DB2 database.
- DB2 stands for ‘Data Base 2’ and it is IBM designed relational database management system.
- Data in DB2 system is stored in form of table, that contains rows(tuples) and columns(attributes).
- DB2 system is almost similar to SQL with some added features to support large-scale mainframe applications
- Following topics will help you understand how COBOL program can interface with DB2 systems
- Embedded SQL
- Use of SQL statements within source code of your program is known as Embedded SQL.
- COBOL allows you include Embedded SQL statements in your COBOL programs. Any language interfacing with DB2 is referred to as Host Language. In our case COBOL is Host Language.
- This feature allows COBOL programmer to directly perform standard SQL operation on database from COBOL program
- Embedded SQL is explained in detail here
- DB2 Application Programming
- Embedded SQL statementi is allowed to program in COBOL.
- Embedded SQL statement must be enclosed between “EXEC SQL” and “END-EXEC”
- DB2 Application programming is explained in detail here
- Host variables
- Host Variables are used to store data that is accessed by both COBOL and DB2.
- Host vatiable allows us to
- Collect data from DB2 table using FETCH, INTO, VALUES INTO etc. clause
- Place data into Host variable and perform INSERT, UDPATE etc. operations
- Use the data in the host variable when evaluating a WHERE or HAVING clause.
- Use the host variable to indicate a null value
- Host variables is explained in detail here
- DCLGEN
- DCLGEN stands for Declarations Generator.
- It is a tool that is used to generate COBOL copybook for DB2 table. This copybook will contain COBOL host variables structure that is map to the columns of DB2 table.
- DCLGEN is explained in detail here
- SQLCA
- SQLCA stands for SQL Communication Area and it is DB2 system provided copybook.
- SQLCA has multiple Host variables defined to hold the information about last executed SQL statement
- All host variables get updated after the execution of each SQL statement.
- Thus, every COBOL program containing Embedded SQL statements must have SQLCA included in WORKING-STORAGE SECTION.
- SQLCA is explained in detail here
- SQL Queries
- Popular SQL statements like SELECT, INSERT, UDPATE DELETE can be used in our COBOL-DB2 Program. It it explained with help of COBOL-DB2 program example here
- DB2 Cursors
- Cursor is used when more than one row of table is to be processed.
- Cursor allows COBOL program to retrieve the set of rows (result set) and then process that returned data one row at a time.
- Cursor can be thought of as data structure that holds all the resultant records of query.
- By using cursor, program can retrieve each row sequentially from the result table until End of Data is reached
- DB2 Cursors is explained in detail here
- Embedded SQL