Vous êtes sur la page 1sur 13

QUICK GUIDE TO SQL AND COBOL WITH DB2 ON WINDOWS NT

By Timothy Paul Cronan, Jennifer Kreie, and Lori Komp Leonard

Quick Guide to SQL and COBOL with DB2/2


Embedding SQL statements in a COBOL program DATA DIVISION (WORKING-STORAGE SECTION) PROCEDURE DIVISION SQL in a PC Network Environment
Using Micro Focus COBOL with DB/2 - SQLPREP and SQLDEMO SQLDEMO2

SQLPREP.CBL program code SQLDEMO.CBL program code SQLDEMO2.CBL program code Tables in the database used in these exercises

Working Storage Section


If the database table ENROLL was defined as CREATE TABLE ENROLL (STUNUM SMALLINT, CLASSNAME CHAR[5], POSNUM CHAR[1]), the corresponding WORKING-STORAGE definition should be:

Working Storage Section


WORKING-STORAGE SECTION.
EXEC SQL BEGIN DECLARE SECTION 01 STUDENTNUMBER PIC S9(3) 01 CLASS PIC X(5). 01 POSITION PIC X(1). EXEC SQL END DECLARE SECTION EXEC SQL INCLUDE SQLCA END-EXEC. COMP-3.

END-EXEC. END-EXEC.

NOTE 1: For each table that you want to access through the application program, you need to have COBOL variablenames defined as illustrated above for the table columns. These COBOL variable-names need to be enclosed within the statements EXEC SQL BEGIN DECLARE SECTION END-EXEC, and the EXEC SQL END DECLARE SECTION END-EXEC. NOTE 2: You need one EXEC SQL INCLUDE SQLCA END-EXEC statement at the end. NOTE 3: The INCLUDE SQLCA creates a communication area between SQL and COBOL, and defines any SQL variable. Numerous Statements are inserted as a result.

Procedure Division
WORKING-STORAGE SECTION. EXEC SQL BEGIN DECLARE SECTION STUID PIC S9(3) STUNAME PIC X(5). STUMAJOR PIC X(1). STUGRADELVL PIC X(2). STUAGE PIC X(2). EXEC SQL END DECLARE SECTION EXEC SQL INCLUDE SQLCA
END-EXEC. COMP-3.

01 01 01 01 01

END-EXEC. END-EXEC.

Procedure Division
PARAGRAPH-1. DISPLAY 'ENTER THE ID NUMBER OF THE STUDENT'. ACCEPT SIDIN FROM CONSOLE. EXEC SQL SELECT SID, NAME, MAJOR, GRADELVL, AGE INTO :STUID, :STUNAME, :STUMAJOR, :STUGRADELVL, :STUAGE FROM STUDENT WHERE SID = :SIDIN END-EXEC.

Cursor
If more than one row is to be extracted from a table, the way COBOL source code is written differs from the earlier illustration. One difference is that the data retrieved from the SQL query returns a table and, therefore, a "CURSOR" is needed.

Cursor
EXEC SQL DECLARE CURSORNAME CURSOR FOR SELECT ................... FROM ..................... Usual SQL statements. WHERE .................... END-EXEC.

Cursor Example
PARAGRAPH-1. DISPLAY 'ENTER DESIRED COURSE NAME'. ACCEPT CLASSNAMEIN FROM CONSOLE. EXEC SQL DECLARE RESULT CURSOR FOR SELECT NAME FROM STUDENT WHERE SID IN (SELECT STUNUM FROM ENROLL WHERE CLASSNAME = :CLASSNAMEIN) END-EXEC. PERFORM PARAGRAPH-2.

Cursor
This SELECT extracts all rows that meet the condition to a table and assigns a cursor named RESULT to that extracted table.
However, instead of opening and closing a data file, the cursor defined in the above SELECT statement is opened and closed. Also, instead of using the usual READ statement, a FETCH statement is used.

Fetch
EXEC SQL OPEN CURSORNAME END-EXEC.

EXEC SQL FETCH CURSORNAME INTO :COBOL-VARIABLE END-EXEC.

Since the FETCH substitutes a READ, this FETCH statement has to be executed until SQLCODE = 100 is returned by SQL. This return code can be viewed as equivalent to the usual END-OF-FILE marker.
PARAGRAPH-2. EXEC SQL OPEN RESULT END-EXEC. PERFORM FETCH-PARAGRAPH. PERFORM PROCESS-LOOP UNTIL SQLCODE = 100. EXEC SQL CLOSE RESULT END-EXEC. FETCH-PARAGRAPH. EXEC SQL FETCH RESULT INTO :CLASSNAMEIN END-EXEC. PROCESS-LOOP. : : Statements to process the names of students : who are enrolled in the specified class. : DISPLAY CLASSNAMEIN PERFORM FETCH-PARAGRAPH.

Vous aimerez peut-être aussi