Chapter 6 Cursors
Chapter 6 Cursors
a b
1 2
3 1
22 12
50 100
10 9
OPEN Statement
FETCH Statement
CLOSE Statement
By dinesh
declare
a1 num.a%type;
b1 num.b%type;
cursor c1 is
select * from num; -- step 1
Begin
open c1; -- step 2
dbms_output.put_line('a' || ' ' || 'b');
dbms_output.put_line('_____________');
loop
fetch c1 into a1,b1;
exit when c1%NOTFOUND; --step 3
dbms_output.put_line(a1 || ' ' || b1);
end loop;
close c1; -- step 4
end;
/
Note:
1. Here I have used *. So we need to declare temp_variables as in the table.
2. If we select 3 columns in SELECT query then we need to declare 3 temp_variable
otherwise error will be raised in red line.
declare
a1 num.a%type;
cursor c1 is
select a from num; -- step 1
Begin
open c1; -- step 2
dbms_output.put_line('a');
dbms_output.put_line('___');
loop
fetch c1 into a1;
exit when c1%NOTFOUND; --step 3
dbms_output.put_line(a1);
end loop;
close c1; -- step 4
end;/
By dinesh
declare
a1 num.a%type;
b1 num.b%type;
cursor c1 is
select a,b from num where a<b; -- step 1
Begin
open c1; -- step 2
dbms_output.put_line('A' || ' ' || 'B');
loop
fetch c1 into a1,b1;
exit when c1%NOTFOUND; --step 3
dbms_output.put_line(a1 ||' '|| b1);
end loop;
close c1; -- step 4
end;
/
//The above program is modified in such a way that when a>b the we need to delete the
current row & to insert row as (b,a) i.e swapping
declare
a1 num.a%type;
b1 num.b%type;
cursor c1 is
select a,b from num where a<b for update; -- step 1
Begin
open c1; -- step 2
dbms_output.put_line('A' || ' ' || 'B');
loop
fetch c1 into a1,b1;
exit when c1%NOTFOUND; --step 3
end loop;
declare
cursor c1 is
select * from num; -- step 1
r_c1 c1%ROWTYPE;
Begin
open c1; -- step 2
loop
fetch c1 into r_c1;
exit when c1%NOTFOUND; --step 3
if r_c1.a > r_c1.b then
dbms_output.put_line(r_c1.a || ' is > than' || r_c1.b);
else
dbms_output.put_line(r_c1.b || ' is > than' || r_c1.a);
end if;
end loop;
close c1; -- step 4
end;
/
Note: in condition you are using two columns a & b. So in SELECT statement you need
to put * or mention both columns.
declare
cursor c1 is
select a from num; -- step 1
r_c1 c1%ROWTYPE;
Begin
open c1; -- step 2
loop
fetch c1 into r_c1;
exit when c1%NOTFOUND; --step 3
if( r_c1.a > 35) then
dbms_output.put_line(r_c1.a || ' is > than ' || 35);
end if;
end loop;
close c1; -- step 4
end;
/
By dinesh
Cursor Attributes
Name Description
%ROWCOUNT Returns number of records fetched from cursor at that point in time.
DECLARE
a1 num.a%type;
b1 num.b%type;
CURSOR C1 IS
SELECT * FROM num
where a>3 and b>10;
BEGIN
OPEN C1;
FETCH C1 INTO a1,b1;
WHILE C1%FOUND
LOOP
FETCH C1 INTO a1,b1;
dbms_output.put_line(a1 || ' '|| b1);
END LOOP;
CLOSE C1;
END;
/
Note: two times we need to fetch the row for while loop one inside & one outside.
By dinesh
A REF CURSOR is basically a data type. A variable created based on such a data type is
generally called a cursor variable. A cursor variable can be associated with different
queries at run-time. The primary advantage of using cursor variables is their capability to
pass result sets between sub programs (like stored procedures, functions, packages etc.).
USING %TYPE
1 declare
2 type r_cursor is REF CURSOR;
3 c1 r_cursor;
4 a1 num.a%type;
5 begin
6 open c1 for select a from num;
7 loop
8 fetch c1 into a1;
9 exit when c1%notfound;
10 dbms_output.put_line(a1);
11 end loop;
12 close c1;
13 end;
/
Explanation:
Step 2 is for defining a cursor r_cursor DATA TYPE which is of type REF CURSOR.
Step 3 Cursor variable is defind of type r_cursor
Step 6 Every cursor must open with associate SELECT statement.
By dinesh
declare
type rc1 is REF CURSOR;
c1 rc1;
r1 supplier%ROWTYPE;
begin
if NOT c1%ISOPEN then
open c1 for select * from supplier;
end if;
loop
FETCH c1 into r1;
exit WHEN c1%NOTFOUND;
dbms_output.put_line(r1.s||' '|| r1.sname||' '||r1.status||'
'||r1.city);
end loop;
close c1;
end;
Output:
By dinesh
Records in Table:
By dinesh
Note:
To create our own data type and specific number of values, We are going to use
TYPE & RECORD.
Note:2
Consider a scenario. When you use ROWTYPE you are selecting all the columns i.e.
one record completely. Now you want to select only specific columns from one record
and whenever you want to use those specific columns anywhere in PLSQL block we have
to use our own record & own data type.
Tips:
consider we are using VIEW instead of table.( for understanding purpose only, this is
not the concept.)
E.g.:
By dinesh
declare
type rc2 is REF CURSOR;
c2 rc2;
loop
fetch c2 into shipment_1;
exit when c2%NOTFOUND;
dbms_output.put_line(shipment_1.sid || ' ' || shipment_1.dest);
end loop;
close c2;
end;
By dinesh
Explanations:
shipment_1 rec1;
The above statement declares a variable "shipment_1er" based on the data type
"rec1." This means that "shipment_1" internally contains the fields "sid" and "dest."
By dinesh
declare
type rc2 is REF CURSOR;
c2 rc2;
type rec1 is record
( sid int,
dest char(20)
);
shipment_1 rec1;
begin
-- 1st query
open c2 for select shipment_id,destination from shipment2 where
shipment_id = 110;
loop
fetch c2 into shipment_1;
exit when c2%NOTFOUND;
dbms_output.put_line(shipment_1.sid || ' ' || shipment_1.dest);
end loop;
close c2;
-- 2nd query
open c2 for select shipment_id,destination from shipment2 where
shipment_id = 114;
loop
fetch c2 into shipment_1;
exit when c2%NOTFOUND;
dbms_output.put_line(shipment_1.sid || ' ' || shipment_1.dest);
end loop;
close c2;
end;
Output:
By dinesh
declare
type rc2 is REF CURSOR;
c2 rc2;
type rec1 is record
( cid int,
dest char(20)
);
rec_ship rec1;
begin
for i in (select shipment_id, ship_date from shipment2) -- any
columns but only 2
loop
open c2 for select cust_id,destination from shipment2
where shipment_id = i.shipment_id;
dbms_output.put_line(i.ship_date);
dbms_output.put_line('__________');
loop
fetch c2 into rec_ship;
exit when c2%NOTFOUND;
dbms_output.put_line(rec_ship.cid || ' '||
rec_ship.dest );
end loop;
close c2;z`
end loop;
end;
Explanation:
for i in (select shipment_id, ship_date from shipment2)
loop
.
.
end loop;
The above loop iterates continuously for each row of the "shipment2" table. The details
of each row in "shipment2t" (like shipment_id, ship_date etc.) will be available in
the variable "i." Using that variable (as part of the SELECT statement), then working
with REF CURSOR as follows:
open c2 for select cust_id,destination from shipment2
where shipment_id = i.shipment_id;
By dinesh
By dinesh
rec_ship rec1;
begin
for i in (select shipment_id, ship_date from shipment2) -- any
columns but only 2
loop
open c2 for select cust_id,destination from shipment2
where shipment_id = i.shipment_id;
dbms_output.put_line(i.ship_date);
dbms_output.put_line('__________');
loop
fetch c2 into rec_ship;
exit when c2%NOTFOUND;
dbms_output.put_line(rec_ship.cid || '
'|| rec_ship.dest );
end loop;
close c2;z`
end loop;
end;
Note:
To use ref cursor in sub programs, put the highlighted code into the procedure. See the
code below:
By dinesh
rec_ship rec1;
procedure p_ref is
begin
loop
fetch c2 into rec_ship;
exit when c2%NOTFOUND;
dbms_output.put_line(rec_ship.dest);
end loop;
end;
begin
for i in (select shipment_id, ship_date from shipment2) -- any
columns but only 2
loop
open c2 for select cust_id,destination from shipment2
where shipment_id = i.shipment_id;
dbms_output.put_line(i.ship_date);
dbms_output.put_line('__________');
p_ref;
end loop;
end;
Explanation:
loop
.
.
p_ref;
.
.
end loop;
According to the above loop, the sub-routine gets executed for every iteration, which
displays the employee information for the respective department.
By dinesh
By dinesh
v Every sub-program (or sub-routine) can accept values passed to it in the form of
"parameters" (or arguments).
v Every parameter is very similar to a variable, but gets declared as part of a sub-
program.
declare
rec_ship rec1;
begin
loop
fetch c2 into rec_ship;
exit when c2%NOTFOUND;
dbms_output.put_line(rec_ship.dest);
end loop;
end;
begin
for i in (select shipment_id, ship_date from shipment2) -- any
columns but only 2
loop
open c2 for select cust_id,destination from shipment2 where
shipment_id = i.shipment_id;
dbms_output.put_line(i.ship_date);
dbms_output.put_line('__________');
p_ref(c2);
end loop;
end;
By dinesh
By dinesh