PL-SQL Lab Assignment 2

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

PL/SQL Lab Assignment-2

1. Write a PL/SQL program to demonstrate following exceptions:


• Too Many Rows
• No Data Found
DECLARE
emp_name employees.ename%TYPE;
emp_salary employees.sal%TYPE;
BEGIN
-- Too Many Rows Exception
SELECT ename, sal INTO emp_name, emp_salary FROM employees WHERE deptno = 10;

EXCEPTION
WHEN TOO_MANY_ROWS THEN
DBMS_OUTPUT.PUT_LINE('Too Many Rows Exception: More than one employee found.');

-- No Data Found Exception


WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('No Data Found Exception: No employee found.');
END;
/
2. Write a PL/SQL code to display a message to check whether the record is deleted or not.
DECLARE
v_empno NUMBER := 100; -- Replace with the employee number to check
BEGIN
DELETE FROM employees WHERE empno = v_empno;

IF SQL%ROWCOUNT > 0 THEN


DBMS_OUTPUT.PUT_LINE('Record deleted successfully.');
ELSE
DBMS_OUTPUT.PUT_LINE('Record not found for deletion.');
END IF;
END;
/
3. Write a PL/SQL code to display a message to provide the information about the number of records
deleted by the delete statement issued in a PL/SQL block.

DECLARE

v_deptno NUMBER := 10; -- Replace with the department number

v_deleted_count NUMBER;

BEGIN

DELETE FROM employees WHERE deptno = v_deptno RETURNING COUNT(*) INTO


v_deleted_count;

DBMS_OUTPUT.PUT_LINE(v_deleted_count || ' records deleted from department ' || v_deptno);

END;

Cursor
EMP (empno, ename, job, sal, deptno)
4. Write a PL/SQL code to demonstrate %TYPE and %ROWTYPE to display details of employees
in EMP table.

DECLARE

v_emp employees%ROWTYPE;

BEGIN

SELECT * INTO v_emp FROM employees WHERE empno = 100; -- Replace with the desired
employee number

DBMS_OUTPUT.PUT_LINE('Employee Details:');

DBMS_OUTPUT.PUT_LINE('Emp No: ' || v_emp.empno);

DBMS_OUTPUT.PUT_LINE('Employee Name: ' || v_emp.ename);

DBMS_OUTPUT.PUT_LINE('Job: ' || v_emp.job);

DBMS_OUTPUT.PUT_LINE('Salary: ' || v_emp.sal);

DBMS_OUTPUT.PUT_LINE('Department No: ' || v_emp.deptno);

END;

/
5. Write a PL/SQL code to display the empno, ename, job of employees of department
number 10 for EMP table (using Cursor).

DECLARE
CURSOR emp_cursor IS

SELECT empno, ename, job FROM employees WHERE deptno = 10;

v_empno employees.empno%TYPE;

v_ename employees.ename%TYPE;

v_job employees.job%TYPE;

BEGIN

OPEN emp_cursor;

LOOP

FETCH emp_cursor INTO v_empno, v_ename, v_job;

EXIT WHEN emp_cursor%NOTFOUND;

DBMS_OUTPUT.PUT_LINE('Emp No: ' || v_empno || ', Name: ' || v_ename || ', Job: ' ||
v_job);

END LOOP;

CLOSE emp_cursor;

END;

/
6. Write a PL/SQL code to display the employee number and name of top 5 highest paid
Employees (using Cursor).
DECLARE
CURSOR top_paid_cursor IS
SELECT empno, ename FROM employees ORDER BY sal DESC FETCH FIRST 5
ROWS ONLY;

v_empno employees.empno%TYPE;
v_ename employees.ename%TYPE;
BEGIN
OPEN top_paid_cursor;
LOOP
FETCH top_paid_cursor INTO v_empno, v_ename;
EXIT WHEN top_paid_cursor%NOTFOUND;
DBMS_OUTPUT.PUT_LINE('Emp No: ' || v_empno || ', Name: ' || v_ename);
END LOOP;
CLOSE top_paid_cursor;
END;
/
7. Write a PL/SQL code to calculate the total salary of first n records of emp table. The value of n is
passed to cursor as parameter

CREATE OR REPLACE PROCEDURE calculate_total_salary(n IN NUMBER) AS


CURSOR emp_cursor IS
SELECT sal FROM employees WHERE ROWNUM <= n;

v_total_salary NUMBER := 0;
v_salary employees.sal%TYPE;
BEGIN
OPEN emp_cursor;
LOOP
FETCH emp_cursor INTO v_salary;
EXIT WHEN emp_cursor%NOTFOUND;
v_total_salary := v_total_salary + v_salary;
END LOOP;
CLOSE emp_cursor;

DBMS_OUTPUT.PUT_LINE('Total Salary of first ' || n || ' records: ' || v_total_salary);


END;
/

-- Example usage:
BEGIN
calculate_total_salary(5); -- Replace 5 with the desired value of n
END;
/

You might also like