0% found this document useful (0 votes)
375 views11 pages

PL / SQL Cursors

The document discusses PL/SQL cursors. There are two types of cursors: implicit and explicit. Implicit cursors are declared by Oracle and are used for DML statements like INSERT, UPDATE, DELETE. Explicit cursors are declared by the user and are used to fetch multiple rows from a SELECT statement. Explicit cursors require opening the cursor, fetching rows one by one into variables, and closing the cursor. Cursor FOR loops provide a shortcut for explicit cursors by automatically performing open, fetch, exit when not found, and close operations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
375 views11 pages

PL / SQL Cursors

The document discusses PL/SQL cursors. There are two types of cursors: implicit and explicit. Implicit cursors are declared by Oracle and are used for DML statements like INSERT, UPDATE, DELETE. Explicit cursors are declared by the user and are used to fetch multiple rows from a SELECT statement. Explicit cursors require opening the cursor, fetching rows one by one into variables, and closing the cursor. Cursor FOR loops provide a shortcut for explicit cursors by automatically performing open, fetch, exit when not found, and close operations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Email Us : sunilkumark11@gmail.

com

PL / SQL Cursors

Cursor is a memory location which is used to run SQL commands.

When an SQL statement is processed, Oracle creates a memory area known as


context area.

A cursor is a pointer to this context area.

It contains all information needed for processing the statement.

In PL/SQL, the context area is controlled by Cursor. A cursor contains


information on a select statement and the rows of data accessed by it.

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: A Cursor declared by Oracle.


2) Explicit Cursors: A Cursor declared by User.

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.

Implicit Cursor Attributes:


---------------------------------------
There are four Implicit cursor attributes
1) SQL%ISOPEN
2) SQL%FOUND
3) SQL%NOTFOUND
4) SQL%ROWCOUNT
1) SQL%ISOPEN:
--------------------------
It is a boolean attribute. It always returns false. It is not used in programming as
it always returns false.

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.

By looking at the above message, we cannot know whether your update


command is effecting the data or not.
To overcome this problem, we have SQL%FOUND attribute.
Have a look at this program

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.

PL/SQL Procedure successfully completed.

Using SQL%NOTFOUND:
------------------------------
SQL%NOTFOUND is exactly opposite to SQL%FOUND.

We rewrite the above program using SQL%NOTFOUND

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.

PL/SQL Procedure successfully completed.


Using SQL%ROWCOUNT:
-----------------------------------

SQL%ROWCOUNT attribute is used to find the no of rows effected by SQL


command.

begin
update emp set sal=2000
where deptno=10;
dbms_output.put_line(SQL%ROWCOUNT||' rows updated');
end;
/

Output:
-----------
3 rows updated.

Note: As a developer, we cannot control the implicit cursor.


We can you these implicit cursor attributes to know whether the command is
effecting the data or not.

Explicit Cursors:
-----------------------
Explicit cursors are used to run select stmt which returns more than one row in a
PL/SQL block

Steps to use Explicit cursors:


------------------------------------

Step 1: Declare the cursor


Step 2: Open the cursor
Srep 3: Fetch the data from the cursor to the local variables
Step 4: close the cursor
Syntax of the above four steps:
-----------------------------------------------

Step 1: Declaring the cursor

cursor < cursor_name>


is < select stmt >;

Ex: cursor c1 is select * from emp;

step 2: Open the cursor : At this point data is loaded in cursor.

open < cursor_name >;

Ex: open c1;

step 3: Fetch the data(records) from the cursor to the local variables

fetch < cursor_name > into < var1 > , < var2> , ....., < varn >;;

Ex: fetch c1 into x,y,z,…;

step 4: close the cursor

close < cursor_name>;

Ex: close c1;

Explicit cursor attributes:


----------------------------------

There are four explicit cursor attributes

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

Note: 1) It is exactly opposite to %FOUND attribute


2) This attribute is used to break the loop of the fetch stmt.

4) %ROWCOUNT:
---------------------------
Returns no of rows fetched by the fetch stmt.

Example of Explicit cursor:


-----------------------------------

1. Write a PL/SQL block to display ename and sal of employees working


in deptno 10

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

Pl/SQL Proceudure successfully completed.

2: Write a PL/SQL procedure to display dname , loc from dept table

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

Pl/SQL Procedure successfully completed.


Ex:
----------
3. Write a PL/SQL block which display ename and sal of employees working in
deptno 10

Declare
cursor c1
is select ename , sal from emp
where deptno=10;
begin

for emp_rec in c1 loop


dbms_output.put_line(emp_rec.ename||'.....'||emp_rec.sal);
end loop;

end;
/

Output:
--------------

CLARK 2450
KING 5000
MILLER 1300

Pl/SQL Proceudure successfully completed.

Note: In the above program emp_rec in implicitly declared record variable,


which is capable of storing one row of the cursor.

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;

ENAME EMPNO SAL


---------- --------- --------
KING 7839 5000
SCOTT 7788 3000
FORD 7902 3000
JONES 7566 2975
BLAKE 7698 2850
CLARK 7782 2450
ALLEN 7499 1600
TURNER 7844 1500
MILLER 7934 1300
WARD 7521 1250
MARTIN 7654 1250
ADAMS 7876 1100
JAMES 7900 950
SMITH 7369 800

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;

NUM_COL1 NUM_COL2 CHAR_COL


-------- -------- --------
5000 7839 KING
3000 7902 FORD
3000 7788 SCOTT
2975 7566 JONES
2850 7698 BLAKE
FOR CURSOR:

DECLARE

Cursor C1 is Select * from emp;

r emp%rowtype;

BEGIN

Open C1;

Loop

Fetch C1 into r;

exit when c1%notfound;

dbms_output.put_line(r.ename||’ ‘||r.sal);

end loop;

close C1;

end;

FOR LOOP CURSOR:

Cursor For loops:


------------------------
It is shortcut way of writing explicit cursors.
When we use cursor for loops , following steps are not required.
1) Open the cursor
2) Fetch stmt
3) exit when condition
4) closing the cursor
5) declaring the local variables

DECLARE

Cursor C1 is Select * from emp;


BEGIN

for r in C1;

loop

dbms_output.put_line(r.ename||’ ‘||r.sal);

end loop;

end;

You might also like