0% found this document useful (0 votes)
36 views6 pages

SQL Cursors for Database Operations

The document contains SQL procedures for various lab assignments involving cursors in database management. It includes tasks such as calculating compound interest for customers, updating employee salaries based on grade, calculating sales tax for customers, reducing course fees based on student enrollment, and increasing employee salaries by 10%. Each section provides the necessary SQL code to create tables and procedures for the specified operations.
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)
36 views6 pages

SQL Cursors for Database Operations

The document contains SQL procedures for various lab assignments involving cursors in database management. It includes tasks such as calculating compound interest for customers, updating employee salaries based on grade, calculating sales tax for customers, reducing course fees based on student enrollment, and increasing employee salaries by 10%. Each section provides the necessary SQL code to create tables and procedures for the specified operations.
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

Database Management Systems - Lab Assignment 08 (Cursors)

Batch A (Roll No: 1–10)


Write a cursor to calculate compound interest for each customer and insert customer id and
compound interest into another table named TEMPLIST.

Tables:
Customer(cust_id, Principal_amount, Rate_of_interest, No_of_years)
TEMPLIST(cust_id, compound_interest)

SQL Code:

CREATE TABLE Customer (


cust_id INT PRIMARY KEY,
Principal_amount DECIMAL(10,2),
Rate_of_interest DECIMAL(5,2),
No_of_years INT
);

CREATE TABLE TEMPLIST (


cust_id INT,
compound_interest DECIMAL(10,2)
);

DELIMITER $$
CREATE PROCEDURE calc_compound_interest()
BEGIN
DECLARE done INT DEFAULT 0;
DECLARE cid INT;
DECLARE p DECIMAL(10,2);
DECLARE r DECIMAL(5,2);
DECLARE n INT;
DECLARE ci DECIMAL(10,2);

DECLARE cur CURSOR FOR


SELECT cust_id, Principal_amount, Rate_of_interest, No_of_years
FROM Customer;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;

OPEN cur;
read_loop: LOOP
FETCH cur INTO cid, p, r, n;
IF done THEN LEAVE read_loop; END IF;

SET ci = p * (POW((1 + r/100), n) - 1);


INSERT INTO TEMPLIST VALUES (cid, ci);
END LOOP;
CLOSE cur;
END$$
DELIMITER ;

Batch A (Roll No: 11–20)


Create a Cursor Emp_Cursor that fetches employee records and increments salary according
to grade.
Increment rules:
- Asst Prof: +10000
- Associate Prof: +20000
- Professor: +30000

SQL Code:

CREATE TABLE EMPLOYEE (


ENo INT PRIMARY KEY,
FName VARCHAR(50),
Age INT,
Grade VARCHAR(20),
Salary DECIMAL(10,2)
);

DELIMITER $$
CREATE PROCEDURE update_salary_by_grade()
BEGIN
DECLARE done INT DEFAULT 0;
DECLARE eno INT;
DECLARE grd VARCHAR(20);
DECLARE sal DECIMAL(10,2);
DECLARE inc DECIMAL(10,2);

DECLARE cur CURSOR FOR SELECT ENo, Grade, Salary FROM


EMPLOYEE;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;

OPEN cur;
read_loop: LOOP
FETCH cur INTO eno, grd, sal;
IF done THEN LEAVE read_loop; END IF;

IF grd = 'Asst Prof' THEN SET inc = 10000;


ELSEIF grd = 'Associate Prof' THEN SET inc = 20000;
ELSEIF grd = 'Professor' THEN SET inc = 30000;
ELSE SET inc = 0;
END IF;

UPDATE EMPLOYEE SET Salary = Salary + inc WHERE ENo = eno;


END LOOP;
CLOSE cur;
END$$
DELIMITER ;

Batch B (Roll No: 31–40)


Write a cursor to find sales tax paid by each customer with his city and state of residence.
Tables:
sales_tax_rate(state, tax_rate)
customer(c_name, product_purchased, price_of_product, city, state)
sales_tax_log(c_name, city, state, sales_tax)

SQL Code:

CREATE TABLE sales_tax_rate (


state CHAR(2) PRIMARY KEY,
tax_rate DECIMAL(5,2)
);

CREATE TABLE customer (


c_name VARCHAR(50),
product_purchased VARCHAR(50),
price_of_product DECIMAL(10,2),
city VARCHAR(50),
state CHAR(2)
);

CREATE TABLE sales_tax_log (


c_name VARCHAR(50),
city VARCHAR(50),
state CHAR(2),
sales_tax DECIMAL(10,2)
);
DELIMITER $$
CREATE PROCEDURE calculate_sales_tax()
BEGIN
DECLARE done INT DEFAULT 0;
DECLARE cname VARCHAR(50);
DECLARE price DECIMAL(10,2);
DECLARE city VARCHAR(50);
DECLARE state CHAR(2);
DECLARE rate DECIMAL(5,2);
DECLARE tax DECIMAL(10,2);

DECLARE cur CURSOR FOR SELECT c_name, price_of_product, city,


state FROM customer;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;

OPEN cur;
read_loop: LOOP
FETCH cur INTO cname, price, city, state;
IF done THEN LEAVE read_loop; END IF;

SELECT tax_rate INTO rate FROM sales_tax_rate WHERE state =


state;
SET tax = (price * rate) / 100;

INSERT INTO sales_tax_log VALUES (cname, city, state, tax);


END LOOP;
CLOSE cur;
END$$
DELIMITER ;

Batch C (Roll No: 41–50)


Create a cursor and reduce fees of all the courses according to the given criteria:
- 10% for >7 students
- 20% for >10 students
- 30% for >15 students
Tables: Student(sno,sname,saddress), Opted(cno,sno), Course(cno,cname,fees)

SQL Code:

DELIMITER $$
CREATE PROCEDURE reduce_course_fees()
BEGIN
DECLARE done INT DEFAULT 0;
DECLARE cid INT;
DECLARE total_students INT;
DECLARE reduction DECIMAL(5,2);

DECLARE cur CURSOR FOR SELECT cno FROM Course;


DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;

OPEN cur;
read_loop: LOOP
FETCH cur INTO cid;
IF done THEN LEAVE read_loop; END IF;

SELECT COUNT(*) INTO total_students FROM Opted WHERE cno =


cid;

IF total_students > 15 THEN SET reduction = 0.30;


ELSEIF total_students > 10 THEN SET reduction = 0.20;
ELSEIF total_students > 7 THEN SET reduction = 0.10;
ELSE SET reduction = 0;
END IF;

UPDATE Course SET fees = fees - (fees * reduction) WHERE cno =


cid;
END LOOP;
CLOSE cur;
END$$
DELIMITER ;

Batch C (Roll No: 51–60)


Write a MySQL cursor to fetch records from the employees table and increase each salary by
10%.

SQL Code:

CREATE TABLE employees (


id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255),
department VARCHAR(255),
salary DECIMAL(10,2)
);

DELIMITER $$
CREATE PROCEDURE increase_salary()
BEGIN
DECLARE done INT DEFAULT 0;
DECLARE eid INT;
DECLARE esalary DECIMAL(10,2);

DECLARE cur CURSOR FOR SELECT id, salary FROM employees;


DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;

OPEN cur;
read_loop: LOOP
FETCH cur INTO eid, esalary;
IF done THEN LEAVE read_loop; END IF;

UPDATE employees SET salary = salary * 1.10 WHERE id = eid;


END LOOP;
CLOSE cur;
END$$
DELIMITER ;

You might also like