A major incompatibility between SQL and the most popular application development languages is that SQL operates on the data of an entire set of table rows at a time, whereas the procedural languages operate on only a single row at a time. A cursor enables SQL to retrieve (or update, or delete) a single row at a time so that you can use SQL in combination with an application written in any of the popular languages.
A cursor is like a pointer that locates a specific table row. When a cursor is active, you can
DELETE the row at which the cursor is pointing.
Cursors are valuable if you want to retrieve selected rows from a table, check their contents, and perform different operations based on those contents. SQL can't perform this sequence of operations by itself. SQL can retrieve the rows, but procedural languages are better at making decisions based on field contents. Cursors enable SQL to retrieve rows from a table one at a time and then feed the result to procedural code for processing. By placing the SQL code in a loop, you can process the entire table row by row.
In a pseudocode representation of embedded SQL, the most common flow of execution looks like this:
EXEC SQL DECLARE CURSOR statement EXEC SQL OPEN statement Test for end of table Procedural code Start loop Procedural code EXEC SQL FETCH Procedural code Test for end of table End loop EXEC SQL CLOSE statement Procedural code
The SQL statements in this listing ...