Sagardbms
Sagardbms
Assignment
6754132uiytSubmited by
Sagar pundir 500125549
EXPERIMENT-01
Q1-Write a PL/SQL code to accept the value of A, B & C display which is greater.
CODE:
BEGIN
DECLARE greatest INT;
IF A > B AND A > C THEN
SET greatest = A;
ELSEIF B > A AND B > C THEN
SET greatest = B;
ELSE
SET greatest = C;
END IF;
RETURN greatest;
END
Q2-Using PL/SQL Statements create a simple loop that display message “Welcome to
PL/SQL Programming” 20 mes.
CODE:
DELIMITER $$
-- Replace 'Your message here' with the message you want to print
SELECT 'Your message here';
SET counter = counter + 1;
END LOOP my_loop;
END $$
DELIMITER ;
DELIMITER ;
CODE:
DELIMITER $$
CREATE PROCEDURE GenerateFibonacci(IN num INT)
BEGIN
DECLARE a INT DEFAULT 0;
DECLARE b INT DEFAULT 1;
DECLARE temp INT;
DECLARE counter INT DEFAULT 1;
SELECT a;
WHILE counter < num DO
SELECT b;
SET temp = b;
SET b = a + b;
SET a = temp;
SET counter = counter + 1;
END WHILE;
END $$
DELIMITER ;
DELIMITER ;
EXPERIMENT-02
Q1- Write a PL/SQL code to accept the value of A, B & C display which is greater using func
ons.
CODE:
DELIMITER $$
RETURN greatest_msg;
END $$
DELIMITER ;
Q2- Using PL/SQL Statements and func ons create a simple loop that display message
“Welcome to PL/SQL Programming” 20 mes.
CODE:
DELIMITER $$
my_loop: LOOP
IF counter > 20 THEN
LEAVE my_loop;
END IF;
-- Concatenate the message to the result
SET result = CONCAT(result, msg, '\n');
SET counter = counter + 1;
END LOOP my_loop;
RETURN result;
END $$
DELIMITER ;
Q3- Write a PL/SQL code block to find the factorial of a number using func ons.
CODE:
DELIMITER $$
DELIMITER ;
Q4- Write a PL/SQL program to generate Fibonacci series using func ons.
CODE:
DELIMITER $$
IF n = 1 THEN
RETURN a;
ELSEIF n = 2 THEN
RETURN b;
END IF;
WHILE i < n - 1 DO
SET temp = b;
SET b = a + b;
SET a = temp;
SET i = i + 1;
END WHILE;
RETURN b;
END $$
DELIMITER ;
Q5- Write a PL/SQL code to find the sum of first N numbers using func ons.
CODE:
DELIMITER $$
WHILE i <= n DO
SET sum_result = sum_result + i;
SET i = i + 1;
END WHILE;
RETURN sum_result;
END $$
DELIMITER ;
22Experiment 3: To understand the concepts of implicit and explicit cursor.
Q. Using implicit cursor update the salary by an increase of 10% for all the records in
EMPLOYEES table, and finally display how many records have been updated. If no records
exist display the message “No Change”.
Code:
DELIMITER $$
BEGIN
UPDATE EMPLOYEES
ELSE
END IF;
END$$
DELIMITER ;
Output :
Using explicit cursor fetch the employee name, employee_id and salary of all the records
from EMPLOYEES table.
Code :
DELIMITER $$
BEGIN
-- Open the
cursor
OPEN emp_cursor;
read_loop: LOOP
IF done = 1 THEN
LEAVE read_loop;
END IF;
END LOOP;
CLOSE emp_cursor;
END$$
DELIMITER ;
Output :
Using explicit cursor Insert the records from EMPLOYEES table for the columns
employee_id, Last_Name and salary for those records whose salary exceeds 2500 into a
new table TEMP_EMP.
Code :
DELIMITER $$
BEGIN
-- Declare a cursor to fetch employee details for those with a salary > 2500
SELECT EMPLOYEE_ID, LAST_NAME, SALARY FROM EMPLOYEES WHERE SALARY > 2500;
OPEN emp_cursor;
read_loop: LOOP
IF done = 1 THEN
LEAVE read_loop;
END IF;
END LOOP;
CLOSE emp_cursor;
END$$
DELIMITER ;
Output :
Exp 4 :
Q1- Create a row level trigger for the customers table that would fire for INSERT or UPDATE
or DELETE opera 7yuz 8zxzzbwons performed on the CUSTOMERS table. This trigger will
display the salary difference between the old values and new values.
CODE:
CREATE TABLE CUSTOMERS (
ID INT PRIMARY KEY,
NAME VARCHAR(50),
AGE INT,
ADDRESS VARCHAR(100),
SALARY DECIMAL(10, 2)
);
COMMIT;
COMMIT;
Exp 5 :