Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
5 views8 pages

Week 9: Queries and Their Outputs

The document outlines various PL/SQL programs aimed at enhancing database management skills through stored procedures and control structures. It includes examples such as finding a course by ID, performing arithmetic operations, calculating the area of a circle, and printing sequences of numbers. Each program is accompanied by its corresponding SQL query and expected output.

Uploaded by

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

Week 9: Queries and Their Outputs

The document outlines various PL/SQL programs aimed at enhancing database management skills through stored procedures and control structures. It includes examples such as finding a course by ID, performing arithmetic operations, calculating the area of a circle, and printing sequences of numbers. Each program is accompanied by its corresponding SQL query and expected output.

Uploaded by

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

Week 9

Objectives: To develop and execute PL/SQL programs, exploring stored


procedures, control structures, and arithmetic operations for dynamic and
efficient database management.

Queries and their Outputs


1. Implement a stored procedure based on COURSE table.
Query:
DELIMITER $$
CREATE PROCEDURE find_course(IN id INT)
BEGIN
SELECT * FROM l1t2 WHERE cno = id;
END $$
DELIMITER ;
CALL find_course(19);
Output:

2. Write a program to print any statement using PL/SQL.


Query:
DELIMITER $$
CREATE PROCEDURE print_statement()
BEGIN
SELECT 'This statement is to be printed';
END$$
DELIMITER ;
CALL print_statement();
Output:

3. Write a program to perform various arithmetic operations in one program.


Query:
DELIMITER $$
CREATE PROCEDURE operations()
BEGIN
DECLARE num1 INT DEFAULT 30;
DECLARE num2 INT DEFAULT 10;
DECLARE addResult INT;
DECLARE subResult INT;
DECLARE mulResult INT;
DECLARE divResult FLOAT;
SET addResult = num1 + num2;
SET subResult = num1 - num2;
SET mulResult = num1 * num2;
SET divResult = num1 / num2;
SELECT addResult AS Addition, subResult AS Subtraction, mulResult AS
Multiplication, divResult AS Division;
END$$
DELIMITER ;
CALL operations();
Output:

4. Write a program to find the area of a circle, get radius as an input from the
user.
Query:
DELIMITER $$
CREATE PROCEDURE circle_area(IN radius FLOAT)
BEGIN
DECLARE area FLOAT;
SET area = PI() * POWER(radius, 2);
SELECT radius AS Radius, area AS Area;
END$$
DELIMITER ;
CALL circle_area(5);
Output:

5. Write a program in PL/SQL to print 1st 10 numbers.


Query:
DELIMITER $$
CREATE PROCEDURE print_numbers()
BEGIN
DECLARE i INT DEFAULT 1;
CREATE TEMPORARY TABLE TempNumbers (Number INT);
WHILE i <= 10 DO
INSERT INTO TempNumbers VALUES (i);
SET i = i + 1;
END WHILE;
SELECT Number FROM TempNumbers;
DROP TEMPORARY TABLE TempNumbers;
END$$
DELIMITER ;
CALL print_numbers();
Output:

6. Write a PL/SQL program to swap to numbers.


Query:
DELIMITER $$
CREATE PROCEDURE swap(IN x INT, IN y INT)
BEGIN
DECLARE z INT;
CREATE TEMPORARY TABLE swapValues (X INT, Y INT);
INSERT INTO swapValues VALUES (x, y);
SET z = x;
SET x = y;
SET y = z;
INSERT INTO swapValues VALUES (x, y);
SELECT * FROM swapValues;
DROP TEMPORARY TABLE swapValues;
END$$
DELIMITER ;
CALL swap(5, 8);
Output:

7. Write a program to print first 7 numbers with a difference of 2 and starting


from 1.
Query:
DELIMITER $$
CREATE PROCEDURE print_numbers2()
BEGIN
DECLARE i INT DEFAULT 1;
DECLARE j INT DEFAULT 1;
CREATE TEMPORARY TABLE TempNumbers (Number INT);
WHILE i <= 7 DO
INSERT INTO TempNumbers VALUES (j);
SET i = i + 1;
SET j = j + 2;
END WHILE;
SELECT Number FROM TempNumbers;
DROP TEMPORARY TABLE TempNumbers;
END$$
DELIMITER ;
CALL print_numbers2();
Output:

8. Write a program to print the value of a variable inside and outside a loop
using LOOP WHEN EXIT statement.
Query:
DELIMITER $$
CREATE PROCEDURE PrintVariableLoop()
BEGIN
DECLARE counter INT DEFAULT 1;
DECLARE message VARCHAR(50);
CREATE TEMPORARY TABLE t1 (Message VARCHAR(50));
SET message = CONCAT('Outside the loop: Counter = ', counter);
INSERT INTO t1 VALUES (message);
loop_label: LOOP
SET message = CONCAT('Inside the loop: Counter = ', counter);
INSERT INTO t1 VALUES (message);
IF counter >= 5 THEN
LEAVE loop_label;
END IF;
SET counter = counter + 1;
END LOOP;
SET message = CONCAT('Outside the loop (after exit): Counter = ', counter);
INSERT INTO t1 VALUES (message);
SELECT * FROM t1;
DROP TEMPORARY TABLE t1;
END$$
DELIMITER ;
CALL PrintVariableLoop();
Output:

9. Write a program to print the value of a variable inside and outside a loop
using LOOP EXIT statement.
Query:
DELIMITER $$
CREATE PROCEDURE PrintVariableLoop2()
BEGIN
DECLARE counter INT DEFAULT 1; DECLARE message VARCHAR(50);
CREATE TEMPORARY TABLE t1 (Message VARCHAR(50));
SET message = CONCAT('Outside the loop: Counter = ', counter);
INSERT INTO t1 VALUES (message);
loop_label: LOOP
SET message = CONCAT('Inside the loop: Counter = ', counter);
INSERT INTO t1 VALUES (message);
IF counter >= 5 THEN
LEAVE loop_label;
END IF;
SET counter = counter + 1;
END LOOP;
SET message = CONCAT('Outside the loop (after exit): Counter = ', counter);
INSERT INTO t1 VALUES (message);
SELECT * FROM t1;
DROP TEMPORARY TABLE t1;
END$$
DELIMITER ;
CALL PrintVariableLoop2();
Output:

You might also like