A DECLARE CURSOR statement associates a cursor name with a query. An
OPEN statement then positions the cursor in front of the first row of the query result
table. A FETCH statement then retrieves the next row of the query and advances the
cursor by one row. Successive executions of a FETCH statement (usually the same
FETCH statement within a program loop) therefore retrieve the first, second, third, etc.
rows from the query result table. When a FETCH statement advances the cursor
beyond the last row, a special variable (SQLSTATE) is set to indicate NOT FOUND.
A cursor is not necessary if a SELECT statement returns only one (or zero) rows.
Conversely, ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month, and much more.