Using Cursors
Cursors provide the primary mechanism for processing the individual rows of a SELECT statement within a stored routine. As described earlier, cursors must be declared (except, of course, when used in a FOR statement) before they can be executed. Once declared, the OPEN statement must be executed to open a cursor so that its result rows can be fetched using the FETCH statement. A cursor is automatically closed by the FETCH call that raises the NOT FOUND exception (the first FETCH call after the last row had been fetched). If a routine completes its use of a cursor before it fetches through the entire result set then it needs to call the CLOSE statement unless the cursor was declared with the WITHRETURN option. The OPEN, FETCH, and CLOSE statements are described below.