When I started exploring DB2-COBOL programs over internet, I realized that we haven’t got a lot of tutorials for Cobol-db2 programs over there. So I thought I can share some of my programs with all of you. My Cobol-DB2 program is very small, but it could help us to understand the structure, how to compile, how to bind and how to run very well.
Step 1. DB2 Table structure.
This is my table it has only two columns, first 3 var char for sequence numbrs and next 15 var char for Name field.
Go to SPUFI create our table using the following SQL statements.
CREATE TABLE MYNAM
( SEQ VARCHAR(03),
INSERT INTO MYNAM VALUES(‘001’,’SHIBU’);
INSERT INTO MYNAM VALUES(‘002’,’SABU’);
INSERT INTO MYNAM VALUES(‘003’,’BALAKRISHNAN’);
INSERT INTO MYNAM VALUES(‘004’,’SREEMATHY’);
Step 2. Execute DCL gen to create copybook for table.
Navigate to DB2I primary options and select DCLGEN
Now Enter table name (mynam) in source table name, location for copybook ( ‘TSHRCI.PGMG.CICOB(mynam)’ in our program )on dataset name field and type ADD in action field .You can leave default values in rest of the fields. Then press enter.
You can verify that, whether copy book generated on the given pds.
Step 3. Code the application. ( cobol db2 sample code)
Step 4. key elements of COBOL DB2 program.
a) Include tables copy book and SQLCA.
We have to include the copy book of table which we desired to use on our app. syntax is follows
Also it is important that including SQL communication area copybook into our program. Syntax follows.
Read more about SQL CA on IBM redbooks
b) Define Host variables and variable for SQL.
Hold on! best practice ahead.
It will be readlly good if you declare SQLCODE variable as following.
01 WS-SQLCODE PIC ——–9.
So whenever a value move into SQLCA, all the negative sql codes will have a – symbol and positive sql codes will be just number, so that we can differentiate both error codes.
Since it is sample program and the intention is to describe the flow of Cobol-DB2 program, I haven’t included the cursors or complex sql statements on our program, I’ll be adding more programs in coming days.
Step 4 Compile and BIND COBOL-DB2 Program
Above Diagram is pretty well describes the whole process of compiling a cobol DB2 program. As I mentioned in the image, precompile step I sno longer required in the latest releases of Cobol, the compiler itself is capable of doing this task.
Step 5. RUN Program
When COBOL-DB2 Program executes, the plan and Package must specified in the SYSIN, When the first sql statement of our program executes, db2 search the collections and consistency token in the provided plan using the provided package name, if the consistency token is not matching gDB2 throw a -805 Error.
//R0318BDJ JOB (12345678),MSGCLASS=H,REGION=4M,