You are on page 1of 4

PL-SQL - 3 Cursors

Page 1

Parameterized Cursors
A cursor can take parameters, which can appear in the associated query wherever
constants can appear. The formal parameters of a cursor must be INparameters.
Therefore, they cannot return values to actual parameters. Also, you cannot impose
the constraint NOTNULLon a cursor parameter.
As the example below shows, you can initialize cursor parameters to default values.
That way, you can pass different numbers of actual parameters to a cursor,
accepting or overriding the default values as you please. Also, you can add new
formal parameters without having to change every reference to the cursor.
DECLARE
CURSORc1(lowINTEGERDEFAULT0,
highINTEGERDEFAULT99)ISSELECT...

The scope of cursor parameters is local to the cursor, meaning that they can be
referenced only within the query specified in the cursor declaration. The values of
cursor parameters are used by the associated query when the cursor is opened.

Passing Cursor Parameters


You use the OPENstatement to pass parameters to a cursor. Unless you want to
accept default values, each formal parameter in the cursor declaration must have a
corresponding actual parameter in the OPENstatement. For example, given the
cursor declaration
DECLARE
emp_nameemp.ename%TYPE;
salaryemp.sal%TYPE;
CURSORc1(nameVARCHAR2,salaryNUMBER)ISSELECT...

any of the following statements opens the cursor:


OPENc1(emp_name,3000);
OPENc1(ATTLEY,1500);
OPENc1(emp_name,salary);

Cursor FOR UPDATE OF and CURRENT OF


CURRENT OF clause is used in an UPDATE or DELETE statement to refer to
the current row of the cursor
The cursor must be declared with the FOR UPDATE OF clause and must be open
and positioned on a row
If the cursor is not open, CURRENT OF clause results in an error
Example of Cursor FOR UPDATE OF and CURRENT OF
create table esal
(empno number,
sal number);
insert into esal
values(1,16000);
insert into esal
values(2,14000);

PL-SQL - 3 Cursors

Page 2

insert into esal


values(3,8000);
insert into esal
values(4,6500);
insert into esal
values(5,9000);
insert into esal
values(6,11000);
insert into esal
values(7,5500);
insert into esal
values(8,3500);
insert into esal
values(9,2200);
insert into esal
values(10,7000);
Multiple updations depending on the salary clause in one pl/sql block
Declare
Cursor cf is
select * from esal
For Update of sal;
M cf%rowtype;
Begin
Open cf;
Loop
Fetch cf into M;
exit when cf%notfound;
If M.Sal >= 16000 Then
M.Sal := 20000;
ElsIf M.Sal >= 14000 Then
M.Sal := 18200;
ElsIf M.Sal >= 12000 Then
M.Sal := 16700;
ElsIf M.Sal >= 10000 Then
M.Sal := 13500;

PL-SQL - 3 Cursors
ElsIf M.Sal >= 8000 Then
M.Sal := 11000;
ElsIf M.Sal >= 6000 Then
M.Sal := 9500;
ElsIf M.Sal >= 4000 Then
M.Sal := 7500;
Else
M.Sal := 5000;
End If;
Update esal
set sal = M.Sal
Where Current Of cf;
End Loop;
End;

Page 3

PL-SQL - 3 Cursors

Cursors with Parameters

1) Pass parameter values to a cursor when the cursor


is opened and the query is executed.
2) Open an explicit cursor several times with a
different active set each time.
OPEN cursor_name(parameter_value,.....) ;

Example of Parameterised Cursor


declare
cursor cf(pjob emp.job%type)
is
select empno,ename,job,sal
from emp
where job = pjob;
M cf%rowtype;
begin
open cf('ANALYST');
LOOP
FETCH CF INTO M;
EXIT WHEN CF%NOTFOUND;
dbms_output.put_line(M.ename);
end loop;
close cf;
open cf('CLERK');
LOOP
FETCH CF INTO M;
EXIT WHEN CF%NOTFOUND;
dbms_output.put_line(M.ename);
end loop;
close cf;
END;

Page 4