PL-SQL Lab Assignment 2
PL-SQL Lab Assignment 2
PL-SQL Lab Assignment 2
EXCEPTION
WHEN TOO_MANY_ROWS THEN
DBMS_OUTPUT.PUT_LINE('Too Many Rows Exception: More than one employee found.');
DECLARE
v_deleted_count NUMBER;
BEGIN
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:');
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
v_empno employees.empno%TYPE;
v_ename employees.ename%TYPE;
v_job employees.job%TYPE;
BEGIN
OPEN emp_cursor;
LOOP
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
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;
-- Example usage:
BEGIN
calculate_total_salary(5); -- Replace 5 with the desired value of n
END;
/