Created
Nov 20, 2025Last Modified
4 months agoAssignment 9 PL/SQL
Assignment 9 PL/SQL Block
1️⃣ Print squares of numbers from 1 to 10 (FOR loop & WHILE loop)
(A) Using FOR Loop
DECLARE
num NUMBER;
BEGIN
FOR num IN 1..10 LOOP
DBMS_OUTPUT.PUT_LINE('Square of ' || num || ' = ' || num*num);
END LOOP;
END;
/
(B) Using WHILE Loop
DECLARE
num NUMBER := 1;
BEGIN
WHILE num <= 10 LOOP
DBMS_OUTPUT.PUT_LINE('Square of ' || num || ' = ' || num*num);
num := num + 1;
END LOOP;
END;
/
2️⃣ Find the reverse of a number
DECLARE
num NUMBER := 12345; -- you can take input using substitution variable (&num)
rev NUMBER := 0;
rem NUMBER;
BEGIN
WHILE num > 0 LOOP
rem := MOD(num, 10);
rev := rev * 10 + rem;
num := TRUNC(num / 10);
END LOOP;
DBMS_OUTPUT.PUT_LINE('Reversed Number = ' || rev);
END;
/
3️⃣ Display employee info by specific employee_id (input from user)
Assuming table: EMPLOYEES(emp_id, emp_name, salary, deptno)
Modify attributes if your table is different.
DECLARE
v_id EMPLOYEES.employee_id%TYPE := &emp_id;
v_name EMPLOYEES.first_name%TYPE;
v_sal EMPLOYEES.salary%TYPE;
v_dept EMPLOYEES.department_id%TYPE;
BEGIN
SELECT first_name, salary, department_id
INTO v_name, v_sal, v_dept
FROM employees
WHERE employee_id = v_id;
DBMS_OUTPUT.PUT_LINE('Employee Name : ' || v_name);
DBMS_OUTPUT.PUT_LINE('Salary : ' || v_sal);
DBMS_OUTPUT.PUT_LINE('Department No : ' || v_dept);
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('No employee found with ID = ' || v_id);
END;
/
4️⃣ Display info of all employees working in deptno 10
DECLARE
CURSOR c_emp IS
SELECT employee_id, first_name, salary
FROM employees
WHERE department_id = 10;
v_id employees.employee_id%TYPE;
v_name employees.first_name%TYPE;
v_sal employees.salary%TYPE;
BEGIN
OPEN c_emp;
LOOP
FETCH c_emp INTO v_id, v_name, v_sal;
EXIT WHEN c_emp%NOTFOUND;
DBMS_OUTPUT.PUT_LINE('ID: ' || v_id ||
' | Name: ' || v_name ||
' | Salary: ' || v_sal);
END LOOP;
CLOSE c_emp;
END;
/
5️⃣ Update salary of employee with employee_id = 101
BEGIN
UPDATE employees
SET salary = salary + 1000
WHERE employee_id = 101;
IF SQL%ROWCOUNT = 0 THEN
DBMS_OUTPUT.PUT_LINE('No employee found with ID 101');
ELSE
DBMS_OUTPUT.PUT_LINE('Salary updated successfully for employee ID 101');
END IF;
END;
/
