|
|
Up and Running with ALLBASE/SQL: HP 3000 and HP 9000 Computer Systems > Chapter 5 Building an ALLBASE/SQL\C Application in HP-UXAdding 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 contains tags for the location of the new elements of SQL code. Use the following command to copy the template to your local directory:
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, as follows:
These will hold data for the individual selections to be displayed by the application for each album. Next, add a cursor declaration, as follows:
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 procedure that is called after the embedded SELECT statement. First, include a call to a procedure that fetches titles:
This line should be inserted immediately after the statements that display data for the Albums table. Next, code the procedure itself, as follows:
Notice that 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. The loop terminates as soon as sqlcode is 100, indicating there are no more rows in the cursor. 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 code 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 the revised application program:
Compile and link as follows on the Series 800:
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"):
|
|