Multirow Queries with Cursor Variables

Now that you have seen the syntax of OPEN FOR and are familiar with cursor variables, let’s explore the nuances involved in multirow queries with NDS.

When you execute an OPEN FOR statement, the PL/SQL runtime engine does the following:

  1. Associates a cursor variable with the query found in the query string

  2. Evaluates any bind arguments and substitutes those values for the placeholders found in the query string

  3. Executes the query

  4. Identifies the result set

  5. Positions the cursor on the first row in the result set

  6. Zeros out the rows-processed count returned by %ROWCOUNT

Note that any bind arguments (provided in the USING clause) in the query are evaluated only when the cursor variable is opened. This means that if you want to use a different set of bind arguments for the same dynamic query, you must issue a new OPEN FOR statement with those arguments.

To perform a multirow query, you follow these steps:

  1. Declare a REF CURSOR type (if one is not already available, as it could be if defined in a package specification)

  2. Declare a cursor variable based on the REF CURSOR

  3. OPEN the cursor variable FOR your query string

  4. Use the FETCH statement to fetch one row at a time from the query

  5. Check cursor attributes (%FOUND, %NOTFOUND, %ROWCOUNT, %ISOPEN) as necessary

  6. Close the cursor variable using the normal CLOSE statement

Here is a simple program to display the specified column of any table for the rows indicated by the WHERE clause (it will work for number, date, ...

Get Oracle PL/SQL Programming, Third Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.