You are on page 1of 14

MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION MUMBAI

A
PROJECT REPORT ON
“CURSORS”
UNDER THE GUIDANCE OF
MR. R.L.KADAM

DEPARTMENT OF COMPUTER ENGINEERING


DR. D. Y. PATIL POLYTECHNIC,
KASABA BAWADA, KOLHAPUR
SEMESTER - III
YEAR: - 2022-23
SUBMITTED BY:-
1. SHRADDHA DHUMAL ROLLNO.2111

2. SAIYAM PATIL ROLLNO.2112

3. TANVI PATIL ROLLNO.2113

4. JANHAVI YADHAV ROLLNO.2114

5. DIKSHA SHINDE ROLLNO.2115


Certificate

This is to certify that Mr. /Ms. ……………………………………………………………………………………………..

Roll No. ……......of ……. Semester of Diploma in ………………………………………….............................

…………………………of Institute, Dr. D. Y. Patil Polytechnic (Code: 0539) has completed the

Micro Project satisfactorily in Subject - .............. ( ............ ) for the academic year 20…..-

20....... as prescribed in the curriculum.

Place: …………………….
Enrollment No : ……………………………………..

Date : ……………………… Exam. Seat No: …………………………………….

Subject Teacher Head of the Department Principal


ACKNOWLEDGMENT

The success & find outcome of this project required a lot of guidance &
assistance from many people and I am extremely privileged to have got all
along the completion of our project. All that we have done is only due to
such supervision & assistance & I would not forget to thanks them.

I owe my deep gratitude to our project guide MR. RANJEET.L.KADAM SIR


who took keen interest on our project work & guided us all along , till the
completion of our project work by providing all the necessary Information
for developing a good system.

I am thankful to & fortunate enough to get constant encouragement,


support & guidance form all teaching staffs of Basic Science which help us in
successful completing our project work.

Date:
Place: Kolhapur.
INDEX

SR. NO. CONTENT PAGE NO.

1 INTRODUCTION TO CURSORS 1
2 IMPLICIT CURSOR 1-2
3 IMPLICIT CURSOR ATTRIBUTES 2-3
4 DRAWBACKS OF IMPLICIT CURSOR 3
5 EXPLICIT CURSOR 3-4
6 DECLARING OPENING & CLOSING A CURSOR 4
7 FETCHING A RECORD FROM CURSOR 4-5
8 CURSOR FOR LOOPS 5-7
9 PARAMETERIZED CURSOR 7-8
DR.D.Y.PATIL POLYTECHNIC ,KASBA BAWADA

TOPIC : CURSORS
PL\SQL cursors provide a way to select multiple row data from the
database and then to process each row individually using a cursors.
We can traverse up and down a result set and retriveonly those rows
which are explicitly requested .Cursors basically help an application
to efficiently use a static result set.
Cursors is a problem to the private area in SQL that stores
information about processing a specific DML statement .The
drawback with select statement is that is returns only one row at a
time in a PL\SQL blocks .Cursors are capable for holding more than
one row. Used for grouping a set of rows and implementing a similar
logic to all the records of that group .
Set of row retrieved in a such a area is called an active day and its size
is depend on number of rows retrieved by search condition of the
query. Oracle reserves a memory area called cursors ,populates this
data with appropriate data and frees the area when task is completed.

Cursors can be classified as :


1.IMPLICIT CURSOR
2.EXPLICIT CURSOR

1.Implicit cursor:-
These cursor is called as internal cursors and are
managed by oracle itself.
2.Explicit cursor:-
These cursor are user defined cursors used for
external processing.

IMPLICIT CURSOR:

Implicit cursor is a session cursor which is opened every time you


run a SELECT or DML statement in the PL/SQL block. An implicit

1
DR.D.Y.PATIL POLYTECHNIC ,KASBA BAWADA

cursor closes after its associated statements run but its attributes
values remain available until another SELECT or DML statements is
executed . Cursor attributes return information about the state of the
cuesor. The syntax for an implicit cursor attribute in SQL attribute
e.g. SQL%found

SQL attribute will always cursor attribute refer to the recently run
DML or SELECT INTO statement .
Implicit Cursor Attributes:
1.SQL%ISOPEN
2.SQL%FOUND
3.SQL%NOTFOUND
4.SQL%ROWCOUNT

1.SQL%ISOPEN:

True if cursor is open or false if cursor has not


been opened or has been closed only used with explicit cursor. Oracle
automatically opens and close implicit cursor associated to any DML
or SELECT statement . Therefore the value for this attribute is always
false.

2.SQL%FOUND:
True if at least one row was processed .If any
DML or SELECT INTO statement returned any row the the value of
this attribute is true, if no rows were returned then the values is false
otherwise when no recent statement is executed then this value is null.

3.SQL%NOTFOUND:
True if no rows were processed . It works
like the SQL%FOUND but contrary results are obtained with
SQL%NOTFOUND that is false if rows are returned ,true if no rows
are returned ,true if no rows are returned and null if no recent
statement is executed.

2
DR.D.Y.PATIL POLYTECHNIC ,KASBA BAWADA

4.SQL%ROWCOUNT:
The number of rows processed by a SQL
statement . If shares null if no recent DML statements are executed
and stores the count of rows affected by the DML statement of select
into statement is executed.

Sr.no Cursor Attributes Description


1. SQL % ISOPEN Returns true if the cursor is open
otherwise false.
2. SQL %FOUND If any row is returned by DML or
select into then true otherwise false.
3. SQL % Returns true when no row is returned
NOTFOUND by DML or select into statement.
4. SQL % Returns the number of rows affected
ROWCOUNT by DML statements.

Drawbacks of implicit cursor :

The implicit cursor has the following drawbacks :


Implicit cursor are less efficient than an explicit cursor .
Implicit cursor are more vulnerable to data errors .
Implicit cursors provide less flexibility to implement programming
control.

Explicit cursor:

The cursor which is declared by user is called Explicit cursor .If select
statements in the PL/SQL returns multiple rows then you have to
create an explicit cursor .The set of rows returned by a multi-row
query is called the result set /Its size is the number of rows that meet
your search criteria.
Cursor cursorname IS SQL select statement;
Explicit cursorcan be controlled using following four control
statements .
1.DECLARE cursor in the declaration section

3
DR.D.Y.PATIL POLYTECHNIC ,KASBA BAWADA

CURSOR c_emp IS SELECT emp_no,emp_name,emp_sal,emp_add


from emp;

2. Open :-
Open statement identifies the active set i.e. query returned
by select statement.
Syntax :- open cursorname;

3.FETCH:-
Fetch statement fetches rows into the variables cursors can
be made into use using cursor for loop |& fetch statement.
Syntax:- fetch cursorname into var1,var2……;

4.CLOSE :-
Close the cursor in the execution section before we end
of the PL/SQL block i.e. close statement closes the cursor.
Syntax:- close cursorname;

CURSOR VARIABLE DESCRIPTION


C%ISOPEN Returns true if the cursor is open
otherwise fase.
C%FOUND If any row is returned by DML or
SELECT into then true otherwise
false.
C%NOTFOUND Returns true when no row is
returned by DML or SELECT
into statement.

Declaring Opening & Closing a cursor:-

You use the Declare,Open &Close statement to control a cursor.


The Declare statement declares the cursor variable.
DECLARE
CURSOR cursor_name IS
SELECT statement;
The open statement executes the query associated with the cursor,
identifies the result set , &position the cursor before the first row.

4
DR.D.Y.PATIL POLYTECHNIC ,KASBA BAWADA

OPEN cursor_name ;
When the last row has been processed the CLOSE statement disables
the cursor.
CLOSE cursor_name;
When a cursor is opened the first row becomes the current row.

Fetching a Record from a cursor:

Fetching the cursor involves accessing one row at a time.


When the data is fetched it is copied to the record or variables & the
logical pointer moves to the next row & it becomes the current row.
On every fetch statement the pointer moves to the next row. When
there is more than one rown in a cursor we can use loops along with
explicit cursor attributes to fetch all the records.
We can fetch the rows in a cursor to a PL/SQL .Record or list of
variables created in the PL/SQL block if you are fetching a cursor to
Pl/SQL .record ,the record should have the same structure as the
cursor.
If you are fetching a cursor to a list of variables should be listed in the
same order in the fetch statement as the columns are present in the
cursor.

The syntax is given below:-


FETCH cursor_name INTO record_name;
OR
FETCH cursor_name INTO record_name;

Example:-
DECLARE
Emp_rec emp_tb1%rowtype;
CURSOR emp_cur IS
SELECT *
FROM emp_tb1
WHERE salary>10;
BEGIN
OPEN emp_cur;
FETCH emp_cur INTO emp_rec;

5
DR.D.Y.PATIL POLYTECHNIC ,KASBA BAWADA

Dbms_output.put_line(emp_rec.first_name//’ ‘//emp_rec.last_name);
CLOSE emp_cur;
END;
/

Output:-
Emp_rec.first_name Emp_rec.last_name
Tanvi Patil
Diksha Shinde
Shradha Dhumal

Cursor for loops:-

In most situations that require an explicit cursor ,you can simplify


coding by using a cursor FOR lOOP instead of the Open ,Fetch &
Close statements.
A cursor For loop implicitly declares its loo index as a record that
represents a row fetched from the database .Next , it opens a cursor,
repestedly fetches rows of values from the result set into fields in the
records , then close the cursor when all rows have been processed.
When For loop is used in cursor we do not have to declare a record or
variable to store the cursor values . It is ot necessary to open, fetch &
close the cursor as the For loop automatically performs these actions.

Syntax :
for record IN cursor_name
loop
action
end loop;
end;
/

Example:
Declare
Cursor c1 is select *from emp;
Begin

6
DR.D.Y.PATIL POLYTECHNIC ,KASBA BAWADA

For out variable in c1


Loop
Exit when c1% notfound;
If outvariable.sal<2000 then
Dbms_output.put_line(outvariable.sal ll..lloutvariable.lastname);
End if;
End loop;
End;
Cursor for loop with parameters:
Declare
Cursor c1(dno number)is select *from EMP where deptno=dno;
Begin
For emprec in c1(10) loop;
Dbms_output.put_line(emprec.lastname);
End loop;
End;
/
Output:
Last name Salary
Patil 50000

Parameterized Cursor:

Parameterized cursors are static cursors that can accept passed-in


parameter values when they are opened.
PL/SQL parameterized cursor pass the parameters into a cursor
& use them in to query .Default values is assigned to the cursor
parameters is locally. PL/SQL parameterized cursor define only
datatype.
The following example includes a parameterized cursor . The
cursor displays the name & salary of each employee in the EMP table
whose salary is less than that specified by a passed in parameter
value.

Example:
Declare
my_record emp%ROWTYPE;

7
DR.D.Y.PATIL POLYTECHNIC ,KASBA BAWADA

cursor c1(max_wage number)IS


select *from emp hwere sal<max_wage;
Begin
Open c1(5000);
Loop
Fetch c1 INTO my_record;
EXIT WHEN c1%NOTFOUND;
Dbms_output.put_line(‘Name’ll
my_record.enmaell’salary’llmy_record.sal);
End loop;
Close c1;
End;
/

If 5000 is passed in the value of max_wage ,only the name & salary
data for those employee whose salary is less than 5000 is returned.

Output:
Name Salary
Ram 1500
Krishna 1600
Varad 1550
Manthan 3950

8
DR.D.Y.PATIL POLYTECHNIC ,KASBA BAWADA

References:
www.google.com
www.wikipedia.com

You might also like