With explicit cursors, you must also declare the variable to hold the retrieved records. The Open command within the body of the block causes the cursor to identify the data rows that satisfy the select query, but does not actually retrieve the data. The Fetch command retrieves the query data from the database one row at a time. To process the cursor, the individual fields in the row variable can be referenced using the variable.field format. When processing is completed, the cursor should be closed to recapture memory.
Some queries can get very large, so it is important to close the cursor.
To iterate through multiple records, a loop must be used. One loop structure that can be used is the Exit When. Note that the code below will provide duplicate output for the last record in the table. A preferred approach is to use the FOR IN LOOP format. When you use the FOR IN LOOP format, you do not need to explicitly open, fetch, or close the cursor.