Progress
Embedded SQL-92
Guide and Reference


FETCH Rows Using a Cursor

The FETCH statement advances the cursor to the next row in the active set and retrieves the values from that row. When the FETCH statement is executed for the first time, the cursor is positioned on the first row, and all the column values of the first row are retrieved. Subsequent calls to FETCH retrieve rows one by one from the active set. When all the rows in the active set are retrieved, SQL_NOT_FOUND is set in the SQLCA to indicate that all rows have been retrieved.

EXAMPLE

The following example shows how to use the FETCH statement:

/* 
** CONNECT to the DEFAULT database
*/
EXEC SQL CONNECT TO DEFAULT ;
 
EXEC SQL
     PREPARE cust_sel_stmt
          FROM "SELECT cust_no, last_name, city FROM customer" ;
 
EXEC SQL
     DECLARE cust_cursor CURSOR FOR cust_sel_stmt ;
 
/*
** Open the DECLARED cursor cust_cursor
*/
EXEC SQL OPEN custcur ;
/*
** FETCH rows and print if successful fetch
*/
for (;;)
{
     EXEC SQL
         FETCH cust_cursor
          INTO :cust_no_v, :name_v, :city_v ;
 
     if (sqlca.sqlcode != 0)
          break;
 
     printf ("%d, %s, %s\n", cust_no_v, name_v, city_v) ;
}
 
/* 
** CLOSE the cursor, issue COMMIT to free resources
** and DISCONNECT from the database.
*/
EXEC SQL CLOSE cust_cursor ;
EXEC SQL COMMIT WORK ;
EXEC SQL DISCONNECT DEFAULT ; 


Copyright © 2004 Progress Software Corporation
www.progress.com
Voice: (781) 280-4000
Fax: (781) 280-4095