AIM: Implementing and usage of Cursors:
Cursors:
In SQL when you submit a query, it returns number of rows depends on query. It may be zero or may
be hundreds. While in PL/SQL if your select statement returns multiple rows then oracle must return
Too_many_rows error message (Exception). In Such circumstances it is necessary to manipulate
multiple rows through PL/SQL Block without raising Exception. The resource that Oracle provides to
accomplish this job is the Cursor
Two kinds of cursor are used by Oracle: Implicit and Explicit. PL/SQL implicitly declares a
cursor for every SQL statement. Implicit cursors are declared by Oracle for each UPDATE, DELETE,
and INSERT SQL command. Explicit cursors are declared and used by the user to process multiple
rows returned by a SELECT statement. Explicitly defined cursors are constructs that enable the user to
name an area of memory to hold a specific statement for access at a later time.
Explicit Cursor:
User define cursor are known as Explicit cursor. Explicit cursor is one in which the cursor explicitly
assigned to the select statement. Processing of explicit cursor involves four steps.
1) Declare the cursor
2) Open the cursor
3) Fetch data from cursor
4) Close the cursor
Declaring the cursor:
The first step is to declare cursor in order for PL/SQL to reference the returned data. This must be done
in the declaration portion of your PL/SQL block. Declaring a cursor accomplishes two goals:
It names the cursor
It associates a query with a cursor
The name you assign to a cursor is an undeclared identifier, not a PL/SQL variable. You cannot assign
values to a cursor name or use it in an expression. This name is used in the PL/SQL block to reference
the cursor query.
Cursor is <cursor_name> is <select statement>
Opening the Cursor:
Fetching Data from the Cursor:
Closing the Cursor:
Example
Explicit Cursor Attributes:
Example
Declare
cursor c1 is select * from emp;
Begin
open c1;
if c1%isopen then
dbms_output.put_line(‘cursor already open’);
else
open c1;
end if;
close c1;
End;
/
The %notfound Attribute:
The %notfound attribute is useful in telling you whether a cursor has any rows left in it to be
fetched. The %notfound Attribute equates to true when last fetch statement return no row (there are no
more rows remaining in Active Set), while it equates to false if last fetch statement retuns row. Prior to
the first fetch, this attribute will equate to null. An error will be returned if you evalu- ate %notfound
on a cursor that is not opened.
Following example illustrates use of %notfound attribute.
Example
Declare
cursor cur_emp is select * from emp where ename like ‘A%’;
emp_rec emp%rowtype;
Begin
open cur_emp;
loop
fetch cur_emp into emp_rec;
exit when cur_emp%notfound;
dbms_output.put_line(‘Name : ‘ || emp_rec.ename);
dbms_output.put_line(‘Age : ‘ || round((sysdateemp_rec.bdate)/30, 0);
end loop;
close cur_emp;
End;
/
Above PL/SQL block of cursor will store all the records of employee into active set whose name start
with A and display their name with the age.
The %found Attribute:
The %found attribute equates to true if the last FETCH statement returns row. Therefore, the
%found attribute is a logical opposite of the %notfound attribute. The %found attribute equates to false
when no rows are fetched. Like the %notfound, this attribute also equates to null prior to the first fetch.
The following example illustrates practical use of %found attribute.
Example
Declare
cursor cur_emp is select ename,salary from emp;
nm emp.ename%type;
sal emp.salary%type;
Begin
open cur_emp;
loop
fetch cur_emp into nm,sal;
if cur_emp%found then
dbms_output.put_line(‘Name : ’|| nm);
dbms_output.put_line(‘Salary : ’|| sal);
else
exit;
end if;
end loop;
close cur_emp;
End;
/
The %rowCount Attribute:
The %rowCount attribute returns the number of rows fetched so far for the cursor. Prior to the first
fetch, %rowcount is zero. There are many practical applications of the %rowcount attribute. The
fo
llowing example will perform a commit after the first 250 employees’ salaries are processed.
Example
Declare
cursor c1 is select * from emp where salary > 4000;
emp_rec emp%rowtype;
Begin
open c1;
loop
fetch c1 into emp_rec;
exit when c1%rowcount > 5;
dbms_output.put_line(emp_rec.ename);
end loop;
close c1;
End;
/
Automated Explicit Cursors (Cursor For Loop):
The previous section illustrated the basic mechanism of declaring and using cursors. This also applies
to PL/SQL cursors; there are opportunities to streamline or simplify the coding and usage of them. A
CURSOR FOR loop will implicitly.
Declare the Loop Index
Open the Cursor
Fetch the next row from the cursor for each loop iteration
Close the cursor when all rows are processed or when the loop exits.
The Syntax for cursor for loop :
For <record_list> in <cursor_name>
0 Comments