Receiving More Than One Row Statements that return one row of data include a singleton SELECT and an EXECUTE FUNCTION statement whose user-defined function returns only one row of data. Statements that can return more than one row of data include:
Figure 14-5 SQL Statements That Manage a Select or Function Cursor Task Select Cursor Function Cursor Declare the cursor identifier DECLARE associated with a SELECT statement DECLARE associated with an EXECUTE FUNCTION statement Execute the statement OPEN OPEN Access a single row from the fetch buffer into the program FETCH FETCH Close the cursor CLOSE CLOSE Free cursor resources FREE FREE For more information on any of these statements, see their entries in the Informix Guide to SQL: Syntax. You can change the size of the select or fetch buffer with the Fetch-Buffer-Size feature. For more information, see "Sizing the Cursor Buffer".
Declare the cursor identifier
DECLARE associated with a SELECT statement
DECLARE associated with an EXECUTE FUNCTION statement
Execute the statement
OPEN
Access a single row from the fetch buffer into the program
FETCH
Close the cursor
CLOSE
Free cursor resources
FREE
A Select Cursor A select cursor enables you to scan multiple rows of data that a SELECT statement returns. The DECLARE statement associates the SELECT statement with the select cursor. In the DECLARE statement, the SELECT statement can be in either of the following formats:
Figure 14-6 SQL Statements That Manage an Insert Cursor (1 of 2) Task Insert Cursor Declare the cursor ID DECLARE associated with an INSERT statement Execute the statement OPEN Send a single row from the program into the insert buffer PUT Clear the insert buffer and send the contents to the database server FLUSH Close the cursor CLOSE Free cursor resources FREE For more information on any of these statements, see their entries in the Informix Guide to SQL: Syntax. You can change the size of the insert buffer with the Fetch-Buffer-Size feature. For more information, see "Sizing the Cursor Buffer".
Declare the cursor ID
DECLARE associated with an INSERT statement
Send a single row from the program into the insert buffer
PUT
Clear the insert buffer and send the contents to the database server
FLUSH
Naming the Cursor In an ESQL/C program, you can specify a cursor name with any of the following items:
Figure 14-7 A Sample ESQL/C Function That Uses a Dynamic Cursor
Optimizing Cursor Execution INFORMIX-ESQL/C supports the following features that allow you to minimize network traffic when an ESQL/C application fetches rows from a database server:
2. Retrieves the rows from the database server and puts them in the fetch buffer.
3. Takes the first row out of the fetch buffer and puts the data in the host variables that the user has provided.
2. Check whether more room exists in the insert buffer for subsequent PUT statements.
2. If the size of one row exceeds 4096 bytes, the default buffer size is the size of that row.
Automatically Freeing a Cursor When an ESQL/C application uses a cursor, it usually sends a FREE statement to the database server to deallocate memory assigned to a select cursor once it no longer needs that cursor. Execution of this statement involves a round trip of message requests between the application and the database server. The Automatic-FREE feature (AUTOFREE) reduces the number of round trips by one. When the AUTOFREE feature is enabled, ESQL/C saves a round trip of message requests because it does not need to execute the FREE statement. When the database server closes a select cursor, it automatically frees the memory that it has allocated for it. Suppose you enable the AUTOFREE feature for the following select cursor:
1
Using the SET AUTOFREE Statement You can use the SQL statement, SET AUTOFREE, to enable and disable the AUTOFREE feature. The SET AUTOFREE statement allows you to take the following actions in an ESQL/C program:
Deferring Execution of the PREPARE Statement When an ESQL/C application uses a PREPARE/DECLARE/OPEN statement block to execute a cursor, each statement involves a round trip of message requests between the application and the database server. The Deferred-PREPARE feature reduces the number of round trips by one. When the Deferred-PREPARE feature is enabled, ESQL/C saves a round trip of message requests because it does not need to send a separate command to execute the PREPARE statement. Instead, the database server automatically executes the PREPARE statement when it receives the OPEN statement. Suppose you enable the Deferred-PREPARE feature for the following select cursor:
Enabling the Deferred-PREPARE Feature You can enable the Deferred-PREPARE feature for an ESQL/C application in either of the following ways:
0
Using the SETDEFERRED_PREPARE Statement From within an ESQL/C application you can use the SQL statement, SET DEFERRED_PREPARE, to enable and disable the Deferred-PREPARE feature. The SET DEFERRED_PREPARE statement allows you to take the following actions in an ESQL/C program: