Types of Cursors:: Open Cursor - Name Close Cursor - Name
Types of Cursors:: Open Cursor - Name Close Cursor - Name
We use a cursor when we have a SELECT statement that returns more than one row from the database.
A cursor is basically a set of rows that we can access one at a time.
We retrieve the rows into the cursor using our SELECT statement and then fetch the rows from the cursor.
We may follow five steps when using a cursor:
1. Declare variables to store the column values from the SELECT statement.
2. Declare the cursor, specifying our SELECT statement.
3. Open the cursor.
4. Fetch the rows from the cursor.
5. Close the cursor.
Types of cursors:
1. Implicit Cursor: PL/SQL declares a cursor implicitly for all SQL data manipulation statements,
including queries that return only one row.
2. Explicit Cursor: However, queries that return more than one row you must declare an explicit cursor or
use a cursor FOR loop. Explicit cursor is a cursor in which the cursor name is explicitly assigned to a
SELECT statement via the CURSOR...IS statement.
Cursor Attributes:
%ISOPEN
Syntax: Cursorname%ISOPEN
Returns BOOLEAN Value.
Evaluates to TRUE if the cursor is open else evaluates to FALSE.
%NOTFOUND
Syntax: Cursorname%NOTFOUND
Returns BOOLEAN Value.
Evaluates to TRUE if the most recent fetch does not return a row else evaluates to FALSE.
%FOUND
Syntax: Cursorname%FOUND
Returns BOOLEAN Value.
Evaluates to TRUE if the most recent fetch returns a row else evaluates to FALSE.
%ROWCOUNT
Syntax: Cursorname%ROWCOUNT
Returns BOOLEAN Value.
Evaluates the total number of row returned so far by the cursor.
declare
variable declaration and initialization;
open cursor_name;
fetch cursor_name into user defined variables;
close cursor_name;
end;
Program 1:
declare
v_sno student.sno%TYPE;
v_sname student.sname%TYPE;
v_branch student.branch%TYPE;
v_age student.age%TYPE;
cursor MyCursor is select sno, sname, branch,age from student;
begin
open MyCursor;
fetch MyCursor into v_sno,v_sname,v_branch,v_age;
dbms_output.put_line('Record'||MyCursor%ROWCOUNT||':'||v_sno||' '||v_sname||' '||v_branch||' '||v_age);
fetch MyCursor into v_sno,v_sname,v_branch,v_age;
dbms_output.put_line('Record'||MyCursor%ROWCOUNT||':'||v_sno||' '||v_sname||' '||v_branch||' '||v_age);
fetch MyCursor into v_sno,v_sname,v_branch,v_age;
dbms_output.put_line('Record'||MyCursor%ROWCOUNT||':'||v_sno||' '||v_sname||' '||v_branch||' '||v_age);
fetch MyCursor into v_sno,v_sname,v_branch,v_age;
dbms_output.put_line('Record'||MyCursor%ROWCOUNT||':'||v_sno||' '||v_sname||' '||v_branch||' '||v_age);
close MyCursor;
end;
Program 2:
declare
v_sno student.sno%TYPE;
v_sname student.sname%TYPE;
v_branch student.branch%TYPE;
v_age student.age%TYPE;
cursor MyCursor is select sno, sname, branch,age from student;
begin
open MyCursor;
loop
fetch MyCursor into v_sno,v_sname,v_branch,v_age;
exit when MyCursor%NOTFOUND;
dbms_output.put_line('Record'||MyCursor%ROWCOUNT||':'||v_sno||' '||v_sname||' '||v_branch||' '||v_age);
end loop;
close MyCursor;
end;
Program 3:
declare
cursor MyCursor is select * from student for update of age;
begin
for age_inc in MyCursor
loop
update student set age = (age_inc.age + 2) where current of MyCursor;
end loop;
end;
declare
v_sno student.sno%TYPE;
v_sname student.sname%TYPE;
v_branch student.branch%TYPE;
v_age student.age%TYPE;
cursor MyCursor is select sno, sname, branch,age from student;
begin
if (NOT MyCursor%ISOPEN) then
open MyCursor;
end if;
fetch MyCursor into v_sno,v_sname,v_branch,v_age;
WHILE (MyCursor%FOUND) LOOP
dbms_output.put_line('Record'||MyCursor%ROWCOUNT||':'||v_sno||' '||v_sname||' '||v_branch||' '||v_age);
fetch MyCursor into v_sno,v_sname,v_branch,v_age;
end loop;
IF (MyCursor%ISOPEN) THEN
close MyCursor;
end if;
end;
Program 4:
declare
v_sno student.sno%TYPE;
v_sname student.sname%TYPE;
v_branch student.branch%TYPE;
v_age student.age%TYPE;
cursor MyCursor(c_sno number) is select sno, sname, branch,age from student where sno=c_sno;
--c_record MyCursor%rowtype;
begin
if (NOT MyCursor%ISOPEN) then
open MyCursor(1201);
end if;
loop
fetch MyCursor into v_sno,v_sname,v_branch,v_age;
exit when MyCursor%NOTFOUND;
dbms_output.put_line('Record'||MyCursor%ROWCOUNT||':'||v_sno||' '||v_sname||' '||v_branch||' '||v_age);
end loop;
if (MyCursor%ISOPEN) then
close MyCursor;
end if;
if (NOT MyCursor%ISOPEN) then
open MyCursor(1202);
end if;
loop
fetch MyCursor into v_sno,v_sname,v_branch,v_age;
exit when MyCursor%NOTFOUND;
dbms_output.put_line('Record'||MyCursor%ROWCOUNT||':'||v_sno||' '||v_sname||' '||v_branch||' '||v_age);
end loop;
if (MyCursor%ISOPEN) then
close MyCursor;
end if;
end;