0% found this document useful (0 votes)
22 views4 pages

Exp No 5

The document outlines the creation of a MySQL database and an Employee table, including the insertion of sample records. It details the steps to create a stored procedure that utilizes a cursor to fetch and display employee data from the table. Finally, it explains how to execute the procedure to retrieve the employee information.

Uploaded by

madhukeshs0742
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)
22 views4 pages

Exp No 5

The document outlines the creation of a MySQL database and an Employee table, including the insertion of sample records. It details the steps to create a stored procedure that utilizes a cursor to fetch and display employee data from the table. Finally, it explains how to execute the procedure to retrieve the employee information.

Uploaded by

madhukeshs0742
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/ 4

Experiment 5

Create cursor for Employee table & extract the values from the table. Declare
the variables, Open the cursor & extract the values from the cursor. Close the cursor.
Employee (E_id, E_name, Age, Salary)

Solution:
mysql >CREATE DATABASE COMPANY05;

mysql>USE COMPANY05;

Database changed

1. Creating the Employee Table and insert few records


mysql>CREATETABLE Employee (

E_id INT PRIMARY KEY,

E_name VARCHAR(255),

Age INT,

Salary DECIMAL(10, 2)

);

mysql> INSERT INTO Employee (E_id, E_name, Age, Salary)


VALUES (1, 'Samarth', 30, 50000.00),
(2, 'Ramesh Kumar', 25, 45000.00),
(3, 'Seema Banu', 35, 62000.00),
(4, 'Dennis Anil', 28, 52000.00),
(5, 'Rehman Khan', 32, 58000.00);

Query OK, 5 rows affected (0.01 sec)


Records: 5 Duplicates: 0 Warnings: 0

2. Create a Stored Procedure with Cursor

To create a cursor for the `Employee` table, extract values using the cursor, and then close the
cursor in MySQL, you’ll need to use stored procedures that support cursor operations.
DELIMITER $$
CREATE PROCEDURE fetch_employee_data()
BEGIN
#Declare variables to store cursor values
DECLARE emp_id INT;
DECLARE emp_name VARCHAR(255);
DECLARE emp_age INT;
DECLARE emp_salary DECIMAL(10, 2);
DECLARE finished INT DEFAULT 0;
#Declare a cursor for the Employee table
DECLARE emp_cursor CURSOR FOR
SELECT E_id, E_name, Age, Salary FROM Employee;

#Declare a continue handler for the cursor


DECLARE CONTINUE HANDLER FOR NOT FOUND SET finished = 1;
#Open the cursor
OPEN emp_cursor;
-- Start loop to fetch data from cursor
cursor_loop: LOOP
#Fetch the next row from the cursor into variables
FETCH emp_cursor INTO emp_id, emp_name, emp_age, emp_salary;

#Check if no more rows to fetch


IF finished = 1 THEN
LEAVE cursor_loop;
END IF;

-- Display the extracted employee data


SELECT CONCAT('Employee ID: ', emp_id, ' Name: ', emp_name, ' Age: ',
emp_age, ' Salary: ', emp_salary) AS Employee_Details;
END LOOP;
#Close the cursor
CLOSE emp_cursor;
END $$

DELIMITER;
------------------------------------------------------------------------------------------------------------------
How to Execute & Test the Procedure:
1. Call the procedure to fetch employee data:

CALL fetch_employee_data();
Step Purpose
DELIMITER $$ Changes the delimiter to $$ to allow multi-line SQL statements.
CREATE PROCEDURE Defines a new stored procedure.
BEGIN Starts the procedure body.
DECLARE Variables Stores values retrieved from the Employee table.
DECLARE CURSOR Defines a cursor to iterate over the Employee table.
CONTINUE HANDLER Handles the "no more rows" condition gracefully.
OPEN Cursor Opens the cursor to fetch data.
LOOP Iterates over the rows retrieved by the cursor.
FETCH Retrieves the next row into variables.
IF Condition Stops the loop if no more rows exist.
SELECT Displays employee details in a readable format.
END LOOP Ends the cursor loop.
CLOSE Cursor Closes the cursor to free resources.
END $$ Ends the stored procedure definition.
DELIMITER ; Resets the delimiter to ; for normal queries.
CALL Procedure Executes the stored procedure.

You might also like