|
|
Up and Running with ALLBASE/SQL: HP 3000 and HP 9000 Computer Systems > Chapter 4 Building an ALLBASE/SQL\COBOL Application in MPE/iXAdding a Multiple Row SELECT Statement |
|
Suppose you wanted to display all the selections that are on a particular album in the Albums table in MUSICDBE. So far the application accesses only the Albums table, but you can quickly add the code that will display the multiple selections for each album that are stored in the Titles table. The device used by embedded SQL to handle multiple-row query results in applications is known as a cursor. You use a cursor in a sequence of cursor-related SQL statements:
These have to be added to the source program, together with additional host variable declarations and data display statements for the Titles table. A second template file names contains tags for the location of the new elements of SQL code. COBOL programmers in MPE/iX should use the following command:
Edit the new file, and include the additional statements as shown in the following sections. Add host variable declarations for two additional columns in the Titles table.
These will hold data for the individual selections to be displayed by the application for each album. Next, add a cursor declaration, as follows:
In COBOL, this should be inserted inside a routine within the PROCEDURE DIVISION. After it is declared, a cursor is used in an application when you wish to access a query result that has more than a single row. Use the following statements to read and display multiple rows of data returned:
These statements can be coded inside a COBOL paragraph that is performed after the embedded SELECT statement. Use the following PERFORM statement:
Here is an example of a fetch-and-display procedure in COBOL:
The revised application makes use of the SQLCODE element in the SQLCA to test for the end of the FETCH loop and to determine whether a row was retrieved in the SELECT statement. You should include similar checks following all embedded SQL statements to ensure the desired result. The revised application also includes a COMMIT WORK statement following the CLOSE C1. You must include appropriate COMMIT WORK or ROLLBACK WORK statements to ensure concurrency control and data integrity in your applications, just as you would in an interactive session. You can also include explicit BEGIN WORK statements. For complete information, refer to the chapter "Concurrency Control through Locks and Isolation Levels" in the ALLBASE/SQL Reference Manual. Use the following command to preprocess, compile, and link the revised application program:
Run the revised application, and enter a value of 2008 for an album code. The result should look as follows (assuming that you have set up the database as described in the chapter "Setting up a Database with ISQL"):
|
|