You are on page 1of 4

DB2

COBOL DB2 PROGRAM CONSIDRATION


1. ADD NECESSARY CODE IN THE COBOL PROGRAM
2. COMPILE AND BIND THE PROGRAM
3. CREATE DB2 PLAN
4. EXECUT THE PROGRAM
ADDING NECESSARY CODES IN COBOL FOR DB2-COBOL PROGRAM
1. ADD DB2 T ABLE STRUCTURE IN COBOL PROGRAM
THIS CAN BE DONE IN TWO WAYS:
a) We can add table structure in working storage inside EXEC-SQL & END EXEC
b) Alternative to a) is to use DCLGEN which generates DB2 table equivalent structure which can
be includes in program using
EXEC-SQL
INCLUDE (PDS-MEMBER NAME for DCLGEN generated member for DB2 table)
END-EXEC
2. Declare Host variable in COBOL working storage section which can be further used in program
for passing data (using in SQL) to DB2.o
Host variables are normal COBOL working storage variables which are also used inside EXEC SQL
prefixed with Colon (:).
3. Include SQLCA in working storage section inside EXEC-SQL & END-EXEC. SQLCA serves as
Communication area which can be checked for each executed SQL statement.
4. Define Cursor inside in working storage section if our result table will have more than 1 row.
Cursor definition Syntax:
EXEC-SQL
DEFINCE C1 CURSON FOR
SELECT FNAME,LNAME, SAL, DESIGNATION FROM
EMP WHERE EMP-ID=:WS-EMP
END EXEC.
5. Now in Procedure division we have to Open Cursors to execute SQL statement.
6. Now Fetch returned tables result data into host variable.
7. Close cursor after processing SQL result table.

There are two types of SQL used in Cobol program
a) Static SQL as used in above Cursor example
b) Dynamic SQL
Static SQL provides better performance compared to Dynamic SQL but for changing SQL we have to
make changes in our COBOL program. With use of Host variable in Static SQL statement our SQLs are
flexible while Dynamic SQLs are completely flexible.
Types of Dynamic SQL:
Dynamic SQL for Non-Select Statements: Dynamic SQL without Select clause do not take any main
memory.
These no select dynamic SQLs can be executed in two ways
a) Using Execute Immediate
EXEC SQL
EXECUTE IMMEDIATE :DSTRING;
END-SQL

Here in DSTRING WE CAN MOVE SQL Statement. DSTRING is a host variable.

b) Using Prepare and Execute
Dynamic SQL for Fixed list select
Dynamic SQL for Varying List Select Statement


Working-Storage Section.
01 WS-SQL-COMMAND PIC X(50).
...
PROCEDURE DIVISION.
...
MOVE 'UPDATE STUDENT SET ADDRESS="UNKNOWN" WHERE ROLL_NO=1' TO
WS-SQL-COMMAND.
EXEC SQL
EXECUTE IMMEDIATE :WS-SQL-COMMAND
END-EXEC.




COMPILE AND BIND THE PROGRAM
Cobol DB2 Compilation step goes thru
Pre-compilation
Cobol Program Compile
Cobol Program link edit
DB2 Bind

Pre-compilation step of Cobol DB2 program produces DBRM and Native cobol program(excluding SQLs).
Pre-compilation step check for SQL syntax and adds time stamp in DBRM which is later used to produced
Packages or Plans.
Then Native Cobol program is compiled and link edited.
After Cobol Program is link-edited Binding process takes place and plan is created.
Bind card syntax:
BIND MEMBER (DB2PROG) -
PACKAGE (PACKG11) -
LIBRARY ('MYLIB.DBRM') -
ACTION (REP) -
ISOLATION (CS) -
VALIDATE (BIND)-
RELEASE (COMMIT) -
OWNER (SURESH) -
QUALIFIER (DEVQUALI)
END

You might also like