Go4Expert

Go4Expert (http://www.go4expert.com/)
-   Oracle (http://www.go4expert.com/articles/oracle/)
-   -   Cursors (http://www.go4expert.com/articles/cursors-t4096/)

Sanskruti 1May2007 18:07

Cursors
 
PL/SQL uses cursors for all database information access statements. The language supports the use of both implicit and explicit cursors. Implicit cursors are those established for which explicit cursors are not declared. You must use explicit cursors or cursor FOR loops in all queries that return multiple rows. You define cursors in the variable definition area of PL/SQL subprograms using the CURSOR name IS statement, as shown :

CURSOR c_line_item IS

To use a cursor for manipulating data, you must use the statement OPEN name to execute the query and identify all rows that meet the select criteria. Subsequent retrieval of rows is accomplished with the FETCH statement. Once all information is processed, the CLOSE statement terminates all activity associated with the opened cursor.

The following is an example of cursor control:
Code: SQL

OPEN a_item;

  ...

   FETCH a_item

    INTO b_item;

  ...

  (retrieved row processing)

  ...

CLOSE a_item;

The code opens the cursor a_item and processes the fetched rows. After it retrieves and processes all the information, the cursor closes.

Explicit Cursor Attributes



There are four attributes associated with PL/SQL cursors.
  • %NOTFOUND
  • %FOUND
  • %ROWCOUNT
  • %ISOPEN
All cursor attributes evaluate to TRUE, FALSE, or NULL, depending on the situation. The attribute %NOTFOUND evaluates to FALSE when a row is fetched, TRUE if the last FETCH did not return a row, and NULL if the cursor SELECT returned no data. Attribute %FOUND is the logical opposite of %NOTFOUND with respect to TRUE and FALSE but still evaluates to NULL if the cursor FETCH returns no data.

You can use %ROWCOUNT to determine how many rows have been selected at any point in the FETCH. This attribute increments upon successful selection of a row. In addition, %ROWCOUNT is at zero when the cursor first opens.

The final attribute, %ISOPEN, is either TRUE or FALSE, depending on whether the associated cursor is open. Before the cursor opens and after the cursor closes, %ISOPEN is FALSE. Otherwise, it evaluates to TRUE.

Cursor Parameters



You can specify parameters for cursors in the same way you do for subprograms. The following example illustrates the syntax for declaring parameter cursors:

Code: SQL

CURSOR a_item (order_num IN NUMBER) IS
  SELECT tot_Quantity, recipient_num
  FROM b_item
  WHERE order_num = g_order_num;

The parameter mode is always IN for cursor parameters, but the data type can be any valid data type. You can reference a cursor parameter, whose value is set when the cursor opens, only during the cursor's declared SQL query.

Flexibility within cursor parameters enables the developer to pass different numbers of parameters to a cursor by using the parameter default mechanism. This is illustrated in the following example:

Code: SQL

CURSOR a_item
  (order_num INTEGER DEFAULT 100,
    line_num INTEGER DEFAULT 1) IS ...

By using the INTEGER DEFAULT declaration, you can pass all, one, or none of the parameters to this cursor depending on the logic flow of your code.

Cursor Packages



A cursor package is similar to a procedure package in that you specify the cursor and its return attribute, %TYPE or %ROWTYPE, in the package specification area. You then specify the cursor "body" in the package body specification area. Packaging a cursor in this manner gives you the flexibility of changing the cursor body without having to recompile applications that reference the packaged procedure.

rekha11 1Feb2008 15:32

Re: Cursors
 
There are four attributes associated with PL/SQL cursors.
%NOTFOUND
%FOUND
%ROWCOUNT
%ISOPEN
A cursor package is similar to a procedure package in that you specify the cursor and its return attribute, %TYPE or %ROWTYPE, in the package specification area.

imrantechi 15Feb2008 19:07

Re: Cursors
 
can you please provide certain examples of cursors

lead.smart34 26Feb2008 17:45

Re: Cursors
 
examples please

lead.smart34 26Feb2008 17:46

Re: Cursors
 
i m new cannot understand without examples ,please start with simple to tough ones

harminder 24Jul2008 14:43

Re: Cursors
 
Cursors in PL/SQL

Static cursors
Dynamic cursors
Cursors with parameters
for update
Ref cursors
explicit cursors.
ref cursor from record type.

kidas 25Nov2008 21:46

Re: Cursors
 
Some Additional Info:

Explicit cursor attributes

There are four cursor attributes used in Oracle:
  1. cursor_name%Found
  2. cursor_name%NOTFOUND
  3. cursor_name%ROWCOUNT
  4. cursor_name%ISOPEN

Implicit cursor attributes

Implicit Cursors are prefixed by the word SQL:
  1. SQL%Found
  2. SQL%NOTFOUND
  3. SQL%ROWCOUNT
  4. SQL%ISOPEN
  5. SQL%BULK_ROWCOUNT

Cheers! :D

Night club 22Oct2010 16:06

Re: Cursors
 
CURSOR c_line_item IS

To use a cursor for manipulating data, you must use the statement OPEN name to execute the query and identify all rows that meet the select criteria. Subsequent retrieval of rows is accomplished with the FETCH statement. Once all information is processed, the CLOSE statement terminates all activity associated with the opened cursor.

The following is an example of cursor control:
Code: SQL

It is the major opition.


All times are GMT +5.5. The time now is 19:28.