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

p9.21

The document provides several PL/SQL programs demonstrating various programming concepts including varrays, loops, procedures, cursors, and functions. It includes source code for calculating factorials, displaying customer data, and returning maximum values, among others. Each program is accompanied by a description of its purpose and expected output.

Uploaded by

ukaniprince215
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)
3 views8 pages

p9.21

The document provides several PL/SQL programs demonstrating various programming concepts including varrays, loops, procedures, cursors, and functions. It includes source code for calculating factorials, displaying customer data, and returning maximum values, among others. Each program is accompanied by a description of its purpose and expected output.

Uploaded by

ukaniprince215
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

Practical: 9

Aim: PL/SQL block for loop, arithmetic operations, function and procedure
1. Write a PL/SQL program to illustrates the use of varrays.

SOURCE CODE:

DECLARE

TYPE NameList IS VARRAY(3) OF VARCHAR2(20);

names NameList := NameList('Alice', 'Bob', 'Charlie');

BEGIN

FOR i IN 1..names.COUNT LOOP

DBMS_OUTPUT.PUT_LINE('Name ' || i || ': ' || names(i));

END LOOP;

END;

/
OUTPUT :

2. Write a PL/SQL program that illustrates the concept of loop.

SOURCE CODE:
CHOSALIYA MIT
230280116021

DECLARE
num NUMBER := 5; -- The number for which factorial is calculated
factorial NUMBER := 1; -- Variable to store the factorial result
BEGIN
-- Calculate factorial using a FOR loop
FOR i IN 1..num LOOP
factorial := factorial * i;
END LOOP;
DBMS_OUTPUT.PUT_LINE('Factorial of ' || num || ' is: ' ||
factorial); END;
/

OUTPUT:

3. Write a PL/SQL program to create a simple procedure that displays the string 'Hello World!' on the screen when
executed

SOURCE CODE :
CHOSALIYA MIT
230280116021

DECLARE
-- Declare the procedure
PROCEDURE display_hello_world IS
BEGIN

-- Display 'Hello World!' using DBMS_OUTPUT


DBMS_OUTPUT.PUT_LINE('Hello World!');
END display_hello_world;

BEGIN
-- Call the procedure display_hello_world;

END;
/

OUTPUT :

4. Write a PL/SQL program that makes the use of cursor to display CUSTOMER data in the customers table.

SOURCE CODE :
CHOSALIYA MIT
230280116021

DECLARE

-- Declare variables v_customer_id

customers.customer_id%TYPE;

v_customer_name

customers.customer_name%TYPE; v_customer_email

customers.customer_email%TYPE;

-- Declare the cursor


CURSOR customer_cursor IS
SELECT customer_id, customer_name, customer_email
FROM customers;
BEGIN

-- Open the cursor


OPEN customer_cursor;
-- Fetch and display customer data
LOOP
FETCH customer_cursor INTO v_customer_id, v_customer_name, v_customer_email;

EXIT WHEN customer_cursor%NOTFOUND;

-- Display customer data

DBMS_OUTPUT.PUT_LINE('Customer ID: ' || v_customer_id);


DBMS_OUTPUT.PUT_LINE('Customer Name: ' || v_customer_name);

DBMS_OUTPUT.PUT_LINE('Customer Email: ' || v_customer_email);

DBMS_OUTPUT.PUT_LINE(' ----------------------- ');

END LOOP;
-- Close the cursor
CLOSE customer_cursor;

END;
/
CHOSALIYA MIT
230280116021

OUTPUT :

5. Write a PL/SQL function that returns the total number of CUSTOMERS in the customers table.

SOURCE CODE :
CHOSALIYA MIT
230280116021

CREATE OR REPLACE FUNCTION get_total_customers RETURN NUMBER IS

v_total_customers NUMBER;

BEGIN
-- Query to get the total number of customers
SELECT COUNT(*) INTO v_total_customers FROM customers;
-- Return the total number of customers
RETURN v_total_customers;
END get_total_customers;
/
DECLARE

total_customers NUMBER;
BEGIN
-- Call the function total_customers := get_total_customers;

-- Display the total number of customers


DBMS_OUTPUT.PUT_LINE('Total Number of Customers: ' || total_customers);
END;
/

OUTPUT :

6. Write a PL/SQL Function that computes and returns the maximum of two values.

SOURCE CODE :
CHOSALIYA MIT
230280116021

CREATE OR REPLACE FUNCTION

get_maximum_value( a NUMBER, b NUMBER

) RETURN NUMBER IS v_maximum_value

NUMBER;

BEGIN

-- Calculate the maximum value

IF a >= b THEN v_maximum_value

:= a;

ELSE v_maximum_value

:= b;

END IF;

-- Return the maximum value

RETURN v_maximum_value;

END get_maximum_value;

DECLARE
result NUMBER;
BEGIN

-- Call the function result := get_maximum_value(10, 7);

DBMS_OUTPUT.PUT_LINE('Maximum Value: ' || result);


END;
/
CHOSALIYA MIT
230280116021

OUTPUT :

7. Write a PL/SQL program to calculates the factorial of a given number by calling itself recursively

SOURCE CODE :

CREATE OR REPLACE FUNCTION calculate_factorial(n NUMBER) RETURN NUMBER


IS BEGIN

-- Base case: factorial of 0 is 1


IF n = 0 THEN

RETURN 1;
ELSE
-- Recursive case: n! = n * (n-1)!
RETURN n * calculate_factorial(n - 1);
END IF;

END calculate_factorial;
/
DECLARE
num NUMBER := 5; -- Change this to the desired number result

NUMBER;

BEGIN
-- Call the function to calculate factorial result

:= calculate_factorial(num);

-- Display the result

DBMS_OUTPUT.PUT_LINE('Factorial of ' || num || ' is: ' || result);


END;
/

OUTPUT :

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy