PL / SQL Cursors
PL / SQL Cursors
com
PL / SQL Cursors
A cursor is used to referred to a program to fetch and process the rows returned
by the SQL statement, one at a time. There are two types of cursors:
1) Implicit Cursors: These are declared after the execution of DML command.
----------------------------
The name of the cursor is SQL.
All the activities related to cursor like i) Opening the cursor ii) Processing the
data in the cursor iii) closing the cursor
are done automatically.
Hence these cursors are called Implicit cursors.
2) SQL%FOUND:
-----------------------------
It is a boolean attribute.
Returns TRUE -- if the SQL command effects the data.
Returns FALSE -- if the SQL commands do not effect the data.
3) SQL%NOTFOUND:
--------------------------------
It is a boolean attribute
Returns TRUE -- if the SQL command do not effect the data.
Returns FALSE -- if the SQL command effects the data
Note: It is exactly negation to SQL%FOUND
4) SQL%ROWCOUNT:
-------------------------------
Returns no of rows effected by the SQL command.
Using SQL%FOUND:
-----------------------------
Begin
Update emp set sal=2000
where empno=1111;
end;
/
Output:
-------------
PL/SQL Procedure successfully completed.
Begin
Update emp set sal=2000
where empno=1111;
if SQL%FOUND then
dbms_output.put_line('Update is successfull');
else
dbms_output.put_line('Update is failed');
end if;
end;
/
Output:
------------
Update is failed.
Using SQL%NOTFOUND:
------------------------------
SQL%NOTFOUND is exactly opposite to SQL%FOUND.
Begin
Update emp set sal=2000
where empno=1111;
if SQL%NOTFOUND then
dbms_output.put_line('Update is failed');
else
dbms_output.put_line('Update is successful');
end if;
end;
/
Output:
------------
Update is failed.
begin
update emp set sal=2000
where deptno=10;
dbms_output.put_line(SQL%ROWCOUNT||' rows updated');
end;
/
Output:
-----------
3 rows updated.
Explicit Cursors:
-----------------------
Explicit cursors are used to run select stmt which returns more than one row in a
PL/SQL block
step 3: Fetch the data(records) from the cursor to the local variables
fetch < cursor_name > into < var1 > , < var2> , ....., < varn >;;
1) %ISOPEN
2) %FOUND
3) %NOTFOUND
4) %ROWCOUNT
1) %ISOPEN:
--------------------
It is a boolean attribute.
Returns TRUE -- if the cursor is open
Returns FALSE -- if the cursor is closed
2) %FOUND:
------------------
It is a boolean attribute
Returns TRUE -- if the fetch stmt is successfull
Returns FALSE -- if the fetch stmt fails
3) %NOTFOUND:
--------------------------
It is boolean attribute
Returns TRUE -- if the fetch stmt fails.
Returns FALSE -- if the fetch stmt is successfull
4) %ROWCOUNT:
---------------------------
Returns no of rows fetched by the fetch stmt.
Declare
cursor c1
is select ename , sal from emp
where deptno=10;
l_ename emp.ename%type;
l_sal emp.sal%type;
begin
open c1;
loop
fetch c1 into l_ename , l_sal;
exit when c1%notfound;
dbms_output.put_line( l_ename||'....'||l_sal);
end loop;
close c1;
end;
/
Output:
------------
CLARK 2450
KING 5000
MILLER 1300
Declare
cursor c1
is select dname , loc from dept;
l_dname dept.dname%type;
l_loc dept.loc%type;
begin
open c1;
loop
fetch c1 into l_dname, l_loc;
exit when c1%notfound;
dbms_output.put_line(l_dname||'.....'||l_loc);
end loop;
close c1;
end;
/
Output:
--------------
Accounting New York
Research Dallas
Sales Chicago
Operations Boston
Declare
cursor c1
is select ename , sal from emp
where deptno=10;
begin
end;
/
Output:
--------------
CLARK 2450
KING 5000
MILLER 1300
4.The following example uses a cursor to select the five highest paid
employees from the emp table.
Input Table
SQL> SELECT ename, empno, sal FROM emp ORDER BY sal DESC;
PL/SQL Block
-- available online in file 'sample2'
DECLARE
CURSOR c1 is
SELECT ename, empno, sal FROM emp
ORDER BY sal DESC; -- start with highest paid employee
my_ename VARCHAR2(10);
my_empno NUMBER(4);
my_sal NUMBER(7,2);
BEGIN
OPEN c1;
FOR i IN 1..5 LOOP
FETCH c1 INTO my_ename, my_empno, my_sal;
EXIT WHEN c1%NOTFOUND; /* in case the number requested */
/* is more than the total */
/* number of employees */
INSERT INTO temp VALUES (my_sal, my_empno, my_ename);
COMMIT;
END LOOP;
CLOSE c1;
END;
Output Table
SQL> SELECT * FROM temp ORDER BY col1 DESC;
DECLARE
r emp%rowtype;
BEGIN
Open C1;
Loop
Fetch C1 into r;
dbms_output.put_line(r.ename||’ ‘||r.sal);
end loop;
close C1;
end;
DECLARE
for r in C1;
loop
dbms_output.put_line(r.ename||’ ‘||r.sal);
end loop;
end;