PLSQL Bramhastra
PLSQL Bramhastra
Create a PL/SQL block that declares various IF salary > 50000 THEN WHILE num <= 10 LOOP END LOOP; TYPE employee_record IS RECORD (
data types of variables (e.g., Name , department := 'Sales'; DBMS_OUTPUT.PUT_LINE(num); END; emp_name VARCHAR2(50),
Address, Contact) assigns values, and outputs ELSE num := num + 1; / emp_age NUMBER,
them using DBMS_OUTPUT. department := 'IT'; END LOOP; practical B emp_salary NUMBER
DECLARE END IF; DBMS_OUTPUT.PUT_LINE('Multiplication Table 1.Develop a PL/SQL block that defines a Nested );
v_name VARCHAR2(50); of 15:'); table containing names and prints the
v_address VARCHAR2(100); DBMS_OUTPUT.PUT_LINE('Assigned WHILE multiplier <= 10 LOOP fourthelement from the array. emp employee_record;
v_contact NUMBER(10); Department: ' || department); DBMS_OUTPUT.PUT_LINE('15 x ' || multiplier || DECLARE BEGIN
BEGIN END; ' = ' || (15 * multiplier)); TYPE name_table IS TABLE OF VARCHAR2(20); -- emp.emp_name := 'John Doe';
v_name := 'Ravi Sharma'; / multiplier := multiplier + 1; Define the Nested Table type emp.emp_age := 30;
v_address := '123, MG Road, Mumbai'; c.Write PL/SQL block that check if student END LOOP; names name_table := name_table('John', 'Alice', emp.emp_salary := 50000;
v_contact := 9876543210; First Class, Fail and Pass (Using Else if END; 'Bob', 'Emma', 'Mike'); -- Initialize the Nested
DBMS_OUTPUT.PUT_LINE('Name: ' || v_name); Statements). / Table with names DBMS_OUTPUT.PUT_LINE('Employee Name: ' ||
DBMS_OUTPUT.PUT_LINE('Address: ' || DECLARE D. AIM:- Write a PL/SQL block that uses a while BEGIN emp.emp_name);
v_address); marks NUMBER := 45; loop to display the records from the employee. DBMS_OUTPUT.PUT_LINE('The fourth name is: ' DBMS_OUTPUT.PUT_LINE('Employee Age: ' ||
DBMS_OUTPUT.PUT_LINE('Contact: ' || result VARCHAR2(20); DECLARE || names(4)); -- Print the fourth element from emp.emp_age);
v_contact); BEGIN CURSOR emp_cursor IS -- Declare a cursor to the Nested Table DBMS_OUTPUT.PUT_LINE('Employee Salary: ' ||
END; IF marks >= 60 THEN fetch records from the employee table END; emp.emp_salary);
/ result := 'First Class'; SELECT employee_id, name, department FROM / END;
b.Write a PL/SQL block that performs simple ELSIF marks >= 35 THEN employees; /
arithmetic operations (addition, result := 'Pass'; 2.Develop a PL/SQL block that defines a Nested 2. Define a %ROWTYPE for employee and
subtraction,multiplication and division) and ELSE emp_record emp_cursor%ROWTYPE; cursor table containing number and prints the fourth Create a PL/SQL block that display data from
displays the results. result := 'Fail'; BEGIN element from the array. table.
DECLARE END IF; OPEN emp_cursor; DECLARE DECLARE
num1 NUMBER := 20; LOOP TYPE number_table IS TABLE OF NUMBER; emp_record employees%ROWTYPE;
num2 NUMBER := 5; DBMS_OUTPUT.PUT_LINE('Student Result: ' || FETCH emp_cursor INTO emp_record numbers number_table := number_table(10, 20, BEGIN
sum_result NUMBER; result); EXIT WHEN emp_cursor%NOTFOUND 30, 40, 50); -- Select a record from the employee table
sub_result NUMBER; END; BEGIN (assuming employee_id is a primary key)
mul_result NUMBER; / DBMS_OUTPUT.PUT_LINE('Employee ID: ' || DBMS_OUTPUT.PUT_LINE('The fourth number is: SELECT employee_id, first_name, last_name,
div_result NUMBER; d.Write a PL/SQL block that uses a CASE emp_record.employee_id || ', Name: ' || ' || numbers(4)); salary
BEGIN expression to emp_record.name || ', Department: ' || END; INTO emp_record.employee_id,
sum_result := num1 + num2; determine the name of the day of the week emp_record.department); / emp_record.first_name, emp_record.last_name,
sub_result := num1 - num2; based on a given END LOOP; 3. Create a PL/SQL block that defines a VARRAY emp_record.salary
mul_result := num1 * num2; number (1-7). to store employee names and their FROM employees
div_result := num1 / num2; DECLARE CLOSE emp_cursor; corresponding Employee salary. The program WHERE employee_id = 100; -- Example
day_number NUMBER := 4; END; should use a FOR loop to iterate through the employee_id, change as needed
DBMS_OUTPUT.PUT_LINE('Addition: ' || day_name VARCHAR2(20); / array and print each student's name along
sum_result); BEGIN E : - Write a PL/SQL block that uses a FOR loop with their grade. DBMS_OUTPUT.PUT_LINE('Employee ID: ' ||
DBMS_OUTPUT.PUT_LINE('Subtraction: ' || day_name := CASE day_number to print the numbers from 1 to 10, followed by DECLARE emp_record.employee_id);
sub_result); WHEN 1 THEN 'Sunday' the multiplication table of 15. TYPE emp_name_table IS TABLE OF DBMS_OUTPUT.PUT_LINE('Employee First Name:
DBMS_OUTPUT.PUT_LINE('Multiplication: ' || WHEN 2 THEN 'Monday' BEGIN VARCHAR2(50); ' || emp_record.first_name);
mul_result); WHEN 3 THEN 'Tuesday' FOR num IN 1..10 LOOP TYPE emp_salary_table IS TABLE OF NUMBER; DBMS_OUTPUT.PUT_LINE('Employee Last Name:
DBMS_OUTPUT.PUT_LINE('Division: ' || WHEN 4 THEN 'Wednesday' DBMS_OUTPUT.PUT_LINE(num); emp_names emp_name_table := ' || emp_record.last_name);
div_result); WHEN 5 THEN 'Thursday' END LOOP; emp_name_table('John', 'Alice', 'Bob', 'Emma', DBMS_OUTPUT.PUT_LINE('Employee Salary: ' ||
END; WHEN 6 THEN 'Friday' 'Mike'); emp_record.salary);
c.Write a PL/SQL block that compares two given WHEN 7 THEN 'Saturday' DBMS_OUTPUT.PUT_LINE('Multiplication Table emp_salaries emp_salary_table := END;
numbers and determines the greater of the ELSE 'Invalid Day Number' of 15:'); emp_salary_table(50000, 60000, 45000, 75000, /
two. The block should display the greater END; 55000); Practical A.
number using DBMS_OUTPUT. FOR multiplier IN 1..10 LOOP BEGIN Demonstrate the use of an implicit cursor by
DECLARE DBMS_OUTPUT.PUT_LINE('The day is: ' || DBMS_OUTPUT.PUT_LINE('15 x ' || multiplier || FOR i IN 1..emp_names.COUNT LOOP update employee salary and count the
num1 NUMBER := 45; day_name); ' = ' || (15 * multiplier)); DBMS_OUTPUT.PUT_LINE('Employee: ' || number of employee directly without declaring
num2 NUMBER := 78; END; END LOOP; emp_names(i) || ' - Salary: ' || emp_salaries(i)); a cursor.
BEGIN / END; END LOOP; DECLARE
IF num1 > num2 THEN e.Write a PL/SQL block that uses a CASE / END; emp_count NUMBER;
DBMS_OUTPUT.PUT_LINE('Greater number is: ' expression to determine the F. - Write a PL/SQL block that uses a FOR loop to / BEGIN
|| num1); name of the vowels using (A,E,I,O,U). display the Practical C. -- Update the salary for employees (example:
ELSIF num2 > num1 THEN DECLARE records from the employee table. 1.Create a PL/SQL block that defines an increase salary by 10% for employees in 'Sales'
DBMS_OUTPUT.PUT_LINE('Greater number is: ' char_input CHAR := 'I'; DECLARE associative array containing colour code and department)
|| num2); result VARCHAR2(20); CURSOR emp_cursor IS their associated colour name, and prints the key UPDATE employees
ELSE BEGIN SELECT employee_id, name, department FROM element from the array. ex- R=’Red’, SET salary = salary * 1.10
DBMS_OUTPUT.PUT_LINE('Both numbers are result := CASE char_input employees; G=”Green”. WHERE department_id = 10;
equal.'); WHEN 'A' THEN 'Vowel A' BEGIN DECLARE
END IF; WHEN 'E' THEN 'Vowel E' FOR emp_record IN emp_cursor LOOP TYPE colour_array IS TABLE OF VARCHAR2(20) emp_count := SQL%ROWCOUNT;
END; WHEN 'I' THEN 'Vowel I' DBMS_OUTPUT.PUT_LINE('Employee ID: ' || INDEX BY VARCHAR2(1);
/ WHEN 'O' THEN 'Vowel O' emp_record.employee_id || ', Name: ' || colours colour_array; DBMS_OUTPUT.PUT_LINE('Number of
d.Create a sequence to generate unique WHEN 'U' THEN 'Vowel U' emp_record.name || ', Department: ' || BEGIN employees whose salary was updated: ' ||
employee IDs anddemonstrate its use in ELSE 'Not a Vowel' emp_record.department); colours('R') := 'Red'; emp_count);
inserting new records into the END; END LOOP; colours('G') := 'Green'; END;
'employees' END; colours('B') := 'Blue'; /
table. DBMS_OUTPUT.PUT_LINE('Result: ' || result); / colours('Y') := 'Yellow'; Create an explicit cursor to fetch and display
declare END; Practical 4 colours('P') := 'Purple'; employee employee id , names and salaries
BEGIN / 1.Develop a PL/SQL block that defines a DBMS_OUTPUT.PUT_LINE('Key: R, Colour: ' || from the employees' table.
Practical A. VARRAY containing colour names and prints the colours('R')); DECLARE
INSERT INTO employees (employee_id, name, AIM: Write a PL/SQL block that uses a loop with fourth element from the array. DBMS_OUTPUT.PUT_LINE('Key: G, Colour: ' || CURSOR emp_cursor IS
department) an exit condition to display numbers DECLARE colours('G')); SELECT employee_id, first_name, last_name,
VALUES (emp_seq.NEXTVAL, 'Amit Shah', 'HR'); from 1 to 10. The block should print each TYPE colour_array IS VARRAY(5) OF END; salary
number using DBMS_OUTPUT.PUT_LINE. VARCHAR2(20); -- Define the VARRAY type / FROM employees;
INSERT INTO employees (employee_id, name, DECLARE colours colour_array := colour_array('Red', 2.Create a PL/SQL block that defines an v_employee_id employees.employee_id%TYPE;
department) num NUMBER := 1; -- Starting number 'Green', 'Blue', 'Yellow', 'Purple'); -- Initialize the associative array containing employee name v_first_name employees.first_name%TYPE;
VALUES (emp_seq.NEXTVAL, 'Sneha Mehta', BEGIN VARRAY with colour names and their associated salary, and display all v_last_name employees.last_name%TYPE;
'Finance'); LOOP BEGIN record from the array using while loop. ex- v_salary employees.salary%TYPE;
DBMS_OUTPUT.PUT_LINE(num); -- Print the DBMS_OUTPUT.PUT_LINE('The fourth colour is: ' Sachin=90000,Amrita=80000. BEGIN
INSERT INTO employees (employee_id, name, number || colours(4)); DECLARE OPEN emp_cursor;
department) num := num + 1; -- Increment the number by 1 END; TYPE emp_array IS TABLE OF NUMBER INDEX BY LOOP
VALUES (emp_seq.NEXTVAL, 'Karan Patel', / VARCHAR2(50); FETCH emp_cursor INTO v_employee_id,
'Marketing'); EXIT WHEN num > 10; -- Exit condition: when 2.Develop a PL/SQL block that defines a employees emp_array; v_first_name, v_last_name, v_salary;
num is greater than 10 VARRAY containing number and prints the
DBMS_OUTPUT.PUT_LINE('3 new employees END LOOP; fourth element emp_name VARCHAR2(50); EXIT WHEN emp_cursor%NOTFOUND;
inserted using sequence.'); END; from the array. BEGIN
END; / DECLARE employees('Sachin') := 90000; DBMS_OUTPUT.PUT_LINE('Employee ID: ' ||
/ Practical B. TYPE number_array IS VARRAY(5) OF NUMBER; employees('Amrita') := 80000; v_employee_id);
2a.Write a PL/SQL block that determines AIM: Write a PL/SQL block that uses a loop with numbers number_array := number_array(10, 20, employees('Ravi') := 95000; DBMS_OUTPUT.PUT_LINE('Employee Name: ' ||
whether a given number is even or odd. The an exit condition to display the 30, 40, 50); employees('Nina') := 70000; v_first_name || ' ' || v_last_name);
block should display the result using multiplication table of 15 up to 15 x 10. BEGIN emp_name := 'Sachin'; DBMS_OUTPUT.PUT_LINE('Employee Salary: ' ||
DBMS_OUTPUT. DECLARE DBMS_OUTPUT.PUT_LINE('The fourth number is: WHILE emp_name IS NOT NULL LOOP v_salary);
DECLARE num NUMBER := 1; -- Starting multiplier ' || numbers(4)); DBMS_OUTPUT.PUT_LINE('Employee: ' || DBMS_OUTPUT.PUT_LINE('-----------------------------
num NUMBER := 25; BEGIN / emp_name || ' - Salary: ' || --');
BEGIN LOOP 3. Create a PL/SQL block that defines a VARRAY employees(emp_name)); END LOOP;
IF MOD(num, 2) = 0 THEN DBMS_OUTPUT.PUT_LINE('15 x ' || num || ' = ' to store student names and their corresponding IF emp_name = 'Sachin' THEN CLOSE emp_cursor;
DBMS_OUTPUT.PUT_LINE(num || ' is Even'); || (15 * num)); -- Print multiplication result grades. The program should use a FOR loop to emp_name := 'Amrita'; END;
ELSE num := num + 1; -- Increment the multiplier by 1 iterate through the array and print each ELSIF emp_name = 'Amrita' THEN /
DBMS_OUTPUT.PUT_LINE(num || ' is Odd'); student's emp_name := 'Ravi'; 6. Procedure
END IF; EXIT WHEN num > 10; name along with their grade. ELSIF emp_name = 'Ravi' THEN A. Create store procedure employee and pass 3
END; END LOOP; DECLARE emp_name := 'Nina'; parameter name, contact and address and
/ END; TYPE student_array IS VARRAY(5) OF ELSE display name, contact ,address.
b.Write a PL/SQL block that checks if the salary / VARCHAR2(50); emp_name := NULL; CREATE OR REPLACE PROCEDURE
is greater than 50,000. If so, assign the C - Write a PL/SQL block that uses a while loop TYPE grade_array IS VARRAY(5) OF END IF; display_employee_info (
employee to the "Sales" to print the numbers from 1 to 10, followed by VARCHAR2(2); END LOOP; p_name IN VARCHAR2,
department; otherwise, assign them to the the multiplication table of 15. students student_array := student_array('John', END; p_contact IN VARCHAR2,
"IT" department. The block should DECLARE 'Alice', 'Bob', 'Emma', 'Mike'); / p_address IN VARCHAR2
display the assigned department using num NUMBER := 1; -- Starting number for grades grade_array := grade_array('A', 'B', 'C', 'A', Practical D. ) IS
DBMS_OUTPUT. printing numbers 1 to 10 'B'); 1.Define a record type for employee and Create BEGIN
DECLARE multiplier NUMBER := 1; -- Starting multiplier for BEGIN a PL/SQL block that populates and display these DBMS_OUTPUT.PUT_LINE('Name : ' ||
salary NUMBER := 62000; multiplication table of 15 FOR i IN 1..students.LIMIT LOOP record. p_name);
department VARCHAR2(20); BEGIN DBMS_OUTPUT.PUT_LINE('Student: ' || DBMS_OUTPUT.PUT_LINE('Contact : ' ||
BEGIN -- While loop to print numbers from 1 to 10 students(i) || ' - Grade: ' || grades(i)); DECLARE p_contact);
DBMS_OUTPUT.PUT_LINE('Address : ' || FUNCTION addition(n1 NUMBER, n2 NUMBER)
p_address); RETURN NUMBER IS
END; BEGIN
/ RETURN n1 + n2;
BEGIN END addition;
display_employee_info('John Doe',
'9876543210', '123, Main St'); END my_package;
END; /
/ DECLARE
sum_result NUMBER;
B. Develop a store procedure to delete BEGIN
employee based on employee ID test the my_package.greet('Alice');
procedure
with different input. sum_result := my_package.addition(10, 20);
DBMS_OUTPUT.PUT_LINE('The sum of 10 and 20
C. Develop or create a store procedure to find a is: ' || sum_result);
square number using in and out parameter. END;
/
A.Write a function for addition of two number . B. A function addition(n1 NUMBER, n2
CREATE OR REPLACE FUNCTION NUMBER) RETURN NUMBER, which returns the
add_two_numbers( sum of two numbers.
num1 NUMBER, CREATE OR REPLACE PACKAGE my_package AS
num2 NUMBER FUNCTION addition(n1 NUMBER, n2 NUMBER)
) RETURN NUMBER;
RETURN NUMBER END my_package;
IS /
result NUMBER; CREATE OR REPLACE PACKAGE BODY
BEGIN my_package AS
result := num1 + num2; FUNCTION addition(n1 NUMBER, n2 NUMBER)
RETURN result; RETURN NUMBER IS
END; BEGIN
/ RETURN n1 + n2;
DECLARE END addition;
sum_result NUMBER; END my_package;
BEGIN /
sum_result := add_two_numbers(10, 20); DECLARE
DBMS_OUTPUT.PUT_LINE('The sum is: ' || result NUMBER;
sum_result); BEGIN
END; result := my_package.addition(10, 20);
/ DBMS_OUTPUT.PUT_LINE('The sum is: ' ||
B.Write a function that calculate area of circle . result);
CREATE OR REPLACE FUNCTION END;
calculate_area_of_circle( /
radius NUMBER b. Develop a package named overloading that
) contains : A procedure addition(n1 NUMBER, n2
RETURN NUMBER NUMBER) RETURN NUMBER, which returns the
IS sum of two numbers. A procedure addition(n1
area NUMBER; NUMBER, n2 NUMBER, n3 NUMBER) RETURN
BEGIN NUMBER, which returns the sum of three
area := 3.14159 * radius * radius; -- Using numbers.
3.14159 as an approximation for π CREATE OR REPLACE PACKAGE overloading AS
RETURN area; FUNCTION addition(n1 NUMBER, n2 NUMBER)
END; RETURN NUMBER;
/ FUNCTION addition(n1 NUMBER, n2 NUMBER,
DECLARE n3 NUMBER) RETURN NUMBER;
area_result NUMBER; END overloading;
BEGIN /
area_result := calculate_area_of_circle(5); -- CREATE OR REPLACE PACKAGE BODY overloading
Example with radius 5 AS
DBMS_OUTPUT.PUT_LINE('The area of the circle FUNCTION addition(n1 NUMBER, n2 NUMBER)
is: ' || area_result); RETURN NUMBER IS
END; BEGIN
/ RETURN n1 + n2;
C. Write a function that find a factorial number END addition;
CREATE OR REPLACE FUNCTION
calculate_factorial( FUNCTION addition(n1 NUMBER, n2 NUMBER,
n NUMBER n3 NUMBER) RETURN NUMBER IS
) BEGIN
RETURN NUMBER RETURN n1 + n2 + n3;
IS END addition;
factorial NUMBER := 1; END overloading;
BEGIN /
IF n < 0 THEN DECLARE
RETURN NULL; -- Factorial is not defined for result_two_numbers NUMBER;
negative numbers result_three_numbers NUMBER;
ELSIF n = 0 THEN BEGIN
RETURN 1; -- 0! is 1 result_two_numbers := overloading.addition(10,
ELSE 20);
FOR i IN 1..n LOOP DBMS_OUTPUT.PUT_LINE('The sum of two
factorial := factorial * i; -- Calculate factorial numbers is: ' || result_two_numbers);
iteratively
END LOOP; result_three_numbers :=
RETURN factorial; overloading.addition(10, 20, 30);
END IF; DBMS_OUTPUT.PUT_LINE('The sum of three
END; numbers is: ' || result_three_numbers);
/ END;
DECLARE /
factorial_result NUMBER;
BEGIN
factorial_result := calculate_factorial(5); --
Example with 5
DBMS_OUTPUT.PUT_LINE('The factorial of 5 is: '
|| factorial_result);
END;
/
Package in PL SQL
1. Develop a package named my_package that
contains :
A . A procedure greet(name VARCHAR2) which
prints a greeting message.
CREATE OR REPLACE PACKAGE my_package AS
PROCEDURE greet(name VARCHAR2);
FUNCTION addition(n1 NUMBER, n2 NUMBER)
RETURN NUMBER;
END my_package;
/
CREATE OR REPLACE PACKAGE BODY
my_package AS