This page contains links to additional material that either extends Data Management or keeps it current. There are also some additional exercises.
| Title | VideoYear | Length | Rating | Language | Nationality | Stars |
| Blazing Saddles | 1974 | 93 | R | English | American | 5 |
| The Postman | 1995 | 98 | PG | Italian | Italian | 4.5 |
| Indochine | 1992 | 155 | PG-13 | French | French | 4 |
| Dr. Strangelove | 1964 | 93 | PG | English | American | 5 |
| The Adventures of Priscilla, Queen of the Desert | 1994 | R | English | Australian | 5 | |
| The Magnificent Seven | 1960 | 127 | English | American | 4.5 | |
| Lawrence of Arabia | 1962 | 216 | PG | English | British | 5 |
| Crocodile Dundee | 1986 | 98 | PG-13 | English | Australian | 4 |
| Gandhi | 1982 | 187 | PG | English | British | 5 |
| Like Water for Chocolate | 1992 | 113 | R | Spanish | Mexican | 4 |
In this case, we assume the host language is COBOL and the relational DBMS is DB2. A source program containing SQL statements must be processed by an SQL preprocessor before it is compiled. The preprocessor checks the syntax of the SQL statements, turns them into host language comments, and generates host language statements to invoke the DBMS. Portions of a sample COBOL program for DB2 follow:
FILE SECTION.
******************************************************************
* Non-database files are defined here
******************************************************************
WORKING-STORAGE SECTION.
******************************************************************
* COBOL variables are defined here
******************************************************************
01 VALUE PIC S9(8)V99 COMP-3.
01 TOTAL-VALUE PIC S9(8)V99 COMP-3.
******************************************************************
* Define the SQL communication area
******************************************************************
EXEC SQL INCLUDE SQLCA END-EXEC.
******************************************************************
* Define variables used to transfer data between the
* application and DB2.
******************************************************************
EXEC SQL BEGIN DECLARE SECTION END-EXEC.
01 STOCK-TABLE.
05 STOCK-CODE PIC X(3).
05 STOCK-FIRM PIC X(20).
05 STOCK-PRICE PIC S9(4)V99 COMP-3.
05 STOCK-QTY PIC S9(8) COMP-3.
05 STOCK-DIV PIC S9(3)V99 COMP-3.
05 STOCK-PE PIC S99 COMP-3.
EXEC SQL END DECLARE SECTION END-EXEC.
******************************************************************
PROCEDURE DIVISION.
PERFORM INITIALIZE.
PERFORM READ-ROW
UNTIL SQLCODE NOT = 0.
PERFORM FINISH.
STOP RUN.
INITIALIZE.
* Declare cursor
EXEC SQL
DECLARE STOCK-CURSOR CURSOR FOR
SELECT * FROM STOCK
END-EXEC.
* Set variables
MOVE ZERO TO TOTAL-VALUE.
* Open the cursor
EXEC SQL
OPEN STOCK-CURSOR
END-EXEC.
* Issue an initial FETCH
EXEC SQL
FETCH STOCK-CURSOR INTO :STOCK-TABLE
END-EXEC.
* Check SQLCODE
IF SQLCODE = 100
THEN
DISPLAY Table is empty
STOP RUN
END-IF.
READ-ROW.
* Calculate and accumulate value
VALUE = STOCK-PRICE*STOCK-QTY.
TOTAL-VALUE = TOTAL-VALUE + VALUE.
* Fetch the next row.
EXEC SQL
FETCH STOCK-CURSOR INTO :STOCK-TABLE
END-EXEC.
FINISH.
DISPLAY Total value of stock is TOTAL-VALUE.
EXEC SQL
CLOSE STOCK-CURSOR
END-EXEC.
There are several things to notice about this COBOL program.
| SQLCODE | Condition | Example |
| zero | Normal execution | Successful execution of SQL statement |
| positive | Unusual, but normal condition | End of data (SQLCODE = 100) |
| negative | Abnormal, unexpected condition | Insufficient file space |
There is a fundamental difference between application programming languages, such as COBOL and SQL. COBOL processes one record at a time; SQL processes tables. Thus, an SQL statement will return a table that the COBOL application program then has to process a row or record at a time. This incompatibility is handled by defining a cursor within the application program.
Think of a cursor as a pointer that indicates the row to be processed. In the preceding example, a cursor is defined for processing the STOCK table. The DECLARE statement defines the cursor associated with a particular query
Opening the cursor in the INITIALIZE paragraph is like opening a file; it gets the table ready for processing. The FETCH statement in the INITIALIZE paragraph retrieves the first row of the table. FETCH is like the READ statement of COBOL, which retrieves one row. A looping structure, based on the READ-ROWS paragraph, is then used to fetch subsequent rows, one at a time, until there are no more rows. Then the cursor is closed, again like a file, and the application program stops.
This page is part of the promotional and support material for Data Management (fifth edition) by Richard T. Watson |