Monday, February 6, 2012 8:07

Oracle CURSOR FOR Loop

Tagged with:
Posted by on Monday, April 6, 2009, 8:41
This news item was posted in Oracle category and has 0 Comments so far.

Syntax:

FOR record_index in cursor_name
LOOP
{.statements.}
END LOOP;

You would use a CURSOR FOR Loop when you want to fetch and process every record in a cursor. The CURSOR FOR Loop will terminate when all of the records in the cursor have been fetched.

Here is an example of a function that uses a CURSOR FOR Loop:

CREATE OR REPLACE Function TotalIncome
( name_in IN varchar2 )
RETURN varchar2
IS
total_val number(6);
cursor c1 is
select monthly_income
from employees
where name = name_in;
BEGIN
total_val := 0;
FOR employee_rec in c1
LOOP
total_val := total_val + employee_rec.monthly_income;
END LOOP;
RETURN total_val;
END;

In this example, we’ve created a cursor called c1. The CURSOR FOR Loop will terminate after all records have been fetched from the cursor c1.

Leave a Reply

You can leave a response, or trackback from your own site.