Cursors are set of rows of data fetched from the database with a pointer at the current row which is defined by the iteration as the code parses through the data one row at a time.
When a cursor is used? It provides the unique ability to traverse through the query row-by-row.
Why avoid a cursor? A cursor should be avoided in a situation where the requirement can be achieved by a SQL because it is difficult to optimize to the same extent as the SQL.
How to use Cursors?
- Declare a Cursor and assign it to a query.
- Open the Cursor
- Retrieve the data from the Cursor one row at a time
- Close the cursor.