Assignment 11
Assignment 11
11
Aim
Objective
To study and implement cursor.
Theory
A database stored program (stored module or stored routine) is a computer program that is stored
within and executes within the database server.
When program is executed, it is executed within the memory address of a database server
process or thread.
Syntax
Call example1();
To handle a result set inside a stored procedure, you use a cursor. A cursor allows you to iterate a set of
rows returned by a query and process each row accordingly.
Read only: you cannot update data in the underlying table through the cursor.
Non-scrollable: you can only fetch rows in the order determined by the SELECT statement.
You cannot fetch rows in the reversed order. In addition, you cannot skip rows or jump to
a specific row in the result set.
Asensitive: there are two kinds of cursors: asensitive cursor and insensitive cursor. An
asensitive cursor points to the actual data, whereas an insensitive cursor uses a temporary
copy of the data. An asensitive cursor performs faster than an insensitive cursor because
it does not have to make a temporary copy of data. However, any change that made to the
data from other connections will affect the data that is being used by an asensitive cursor,
therefore, it is safer if you don’t update the data that is being used by an asensitive cursor.
MySQL cursor is asensitive.
Working of Cursor
OPEN cursor_name;
CLOSE cursor_name;
Delimiter $$
Drop procedure if exists empcursor $$
Create procedure empcursor()
Begin
Declare eno int;
Declare ename varchar(20);
Declare esal int;
Declare flag int default 0;
Declare c1 cursor for select eid, empname, Salary from employee;
Declare continue handler for not found set flag = 1;
Open c1;
emp_loop : loop
Fetch c1 into eno, ename, esal;
If flag = 1 then
Leave Stud_loop;
End if;
Output
References:
1. Raghu Ramkrishanan, Johannes Gehrke 4 th Edition “Database Management Systems” 2. Avi
Silberschatz , Henry F. Korth , S. Sudarshan, “Database System Concepts, Sixth Edition”, ISBN-
13: 978-93-3290-138-4, MCGraw Hill
Q. Questions BT CO
No
1 Explain cursors with example? 2 2
2 Explain implicit and explicit cursor? 2 2
Marking criteria.
Experiment completion (Timely)
Lab file (neatness and regularity)
Viva (from time to time)
Mock Practical Exam
Exam (end term): Practical + Viva
Assessment Methodology
Timely completion of assignment- 2marks
Program demonstration- 4 marks
Viva-voce -2 marks
Timely submission of journal- 2 marks