0% found this document useful (0 votes)
24 views

DBMS Ex No 8

The document discusses using a cursor to retrieve the five highest paid employees from an Employee table. It provides details on: 1. Declaring a cursor to select the employee ID and salary ordered by salary descending and limited to 5 rows. 2. Opening the cursor and fetching rows using a loop, retrieving the employee ID and salary into variables. 3. Checking for no more rows using a NOT FOUND handler and displaying the employee ID and salary. 4. Closing the cursor after processing all rows. Sample code is provided to create the table, insert records, define the stored procedure using the cursor, and call the procedure.

Uploaded by

lavanya a
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

DBMS Ex No 8

The document discusses using a cursor to retrieve the five highest paid employees from an Employee table. It provides details on: 1. Declaring a cursor to select the employee ID and salary ordered by salary descending and limited to 5 rows. 2. Opening the cursor and fetching rows using a loop, retrieving the employee ID and salary into variables. 3. Checking for no more rows using a NOT FOUND handler and displaying the employee ID and salary. 4. Closing the cursor after processing all rows. Sample code is provided to create the table, insert records, define the stored procedure using the cursor, and call the procedure.

Uploaded by

lavanya a
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

EX NO: 8.

Cursor Creation
AIM:
To create a database and apply cursor.
Query:
Create an Employee table with an attributes { empid, empname, department
designation, Gross_salary . Using cursor, select the five highest paid employees
from the Employee table.
Introduction to MySQL cursor:
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 individually.
MySQL cursor is read-only, non-scrollable and asensitive.
 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: An asensitive cursor points to the actual data. An asensitive
cursor performs faster. 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 do not update the data that is being used
by an asensitive cursor.
You can use MySQL cursors in stored procedures, stored functions, and triggers.
Working with MySQL cursor:
I. Declare a cursor by using the DECLARE statement:
Syntax:
DECLARE cursor_name CURSOR FOR SELECT_statement;
The cursor declaration must be after any variable declaration.
II. Open the cursor by using the OPEN statement. The OPEN statement
initializes the result set for the cursor, therefore, you must call
the OPEN statement before fetching rows from the result set.
Syntax:
OPEN cursor_name;
III. FETCH statement to retrieve the next row pointed by the cursor and move
the cursor to the next row in the result set.
Syntax:
FETCH cursor_name INTO variables list;
IV. Deactivate the cursor and release the memory associated with it using
the CLOSE statement:
Syntax:
CLOSE cursor_name;
When working with MySQL cursor, you must also declare a NOT FOUND handler
to handle the situation when the cursor could not find any row.
To declare a NOT FOUND handler, you use the following syntax:
DECLARE CONTINUE HANDLER FOR NOT FOUND SET finished = 1;
The following diagram illustrates how MySQL cursor works.

List of Queries:
1. Create table employee_details with fields employee id, name, department,
designation, salary.
2. Insert 6 records for the table.
3. Create stored procedure highest_salary() using explicit cursor to get 5
highest salary.
4. Call procedure to display the result.
Result:
Thus the database was created and cursor was applied successfully.

Left side content:


1.
create table employee_details
(
eid int primary key,
emp_name varchar(20),
dept_name varchar(10),
designation varchar(30),
salary int
);
2.
insert into employee_details values(5,'sweety','cse','tester',40000);
insert into employee_details values(4,'sai','cse','programmer',50000);
insert into employee_details values(1,'mala','ece','trainer',10000);
insert into employee_details values(2,'saratha','eee','liner',17000);
insert into employee_details values(3,'mathi','cse','designer',25000);
insert into employee_details values(8,'chitra','civil','supervisor',35000);
3.
delimiter $$
create procedure highest_salary()
begin
declare eid_var integer;
declare salary_var integer;
declare v_finished integer default 0;
declare c_emp cursor for select eid,salary from employee_details order by salary
desc limit 5;
declare continue handler for NOT FOUND set v_finished=1;
open c_emp;
get_emp:loop
fetch c_emp into eid_var,salary_var;
if v_finished=1 then
leave get_emp;
end if;
select concat(eid_var,concat('-',salary_var));
end loop get_emp;
close c_emp;
end $$
4.
call highest_salary();

4-50000
5-40000
8-35000
3-25000
2-17000

You might also like