plsql

You might also like

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

q3 .

SET NULL "NULL";


SET SERVEROUTPUT ON;

DECLARE
DEPTID DEPT.DEPT_ID%TYPE;
DEPTHEAD DEPT.DEPT_HEAD%TYPE;
BEGIN
-- Selecting DEPT_ID and DEPT_HEAD for a department with no employees
SELECT D.DEPT_ID, D.DEPT_HEAD
INTO DEPTID, DEPTHEAD
FROM DEPT D
WHERE D.DEPT_ID NOT IN (SELECT E.DEPT_ID FROM EMP E WHERE E.DEPT_ID IS NOT
NULL)
AND ROWNUM = 1; -- Ensure only one row is selected

-- Update statement to assign DEPT_ID and DEPT_HEAD to active employees with


NULL DEPT_ID
UPDATE EMP
SET DEPT_ID = DEPTID, MGR_ID = DEPTHEAD
WHERE DEPT_ID IS NULL
AND UPPER(EMP_STATUS) = 'ACTIVE';

-- Cursor to select and display the updated active employees with their details
DECLARE
CURSOR C IS
SELECT E.EMP_ID AS emp_id,
(E.EMP_FNAME || ' ' || E.EMP_LNAME) AS emp_name,
E.EMP_STATUS AS emp_status,
E.DEPT_ID AS dept_id,
D.DEPT_NAME AS dept_name,
(M.MGR_FNAME || ' ' || M.MGR_LNAME) AS mgr_name
FROM EMP E
INNER JOIN DEPT D ON D.DEPT_ID = E.DEPT_ID
INNER JOIN MGR M ON D.DEPT_HEAD = M.DEPT_HEAD
WHERE UPPER(E.EMP_STATUS) = 'ACTIVE'
AND E.DEPT_ID = DEPTID;
BEGIN
DBMS_OUTPUT.PUT_LINE('Emp_id' || ' ' || 'Emp_name' || ' ' || 'Emp_status'
|| ' ' || 'Dept_id' || ' ' || 'Dept_name' || ' ' || 'Mgr_name');
FOR i IN C LOOP
DBMS_OUTPUT.PUT_LINE(i.emp_id || ' ' || i.emp_name || ' ' ||
i.emp_status || ' ' || i.dept_id || ' ' || i.dept_name || ' ' || i.mgr_name);
END LOOP;
END;
END;
/

Q2 .
SET NULL "NULL";
SET FEEDBACK OFF;
SET ECHO OFF;
SET HEADING OFF;
SET WRAP OFF;
SET LINESIZE 10000;
SET TAB OFF;
SET PAGES 0;
SET DEFINE OFF;
set serveroutput on;
declare incentive number;
work_exp number;
cursor curr is select emp_id, concat(emp_fname,concat(' ',emp_lname)) as empname,
emp_hiredate from emp where emp_status='Active' and extract(month from
to_date(emp_hiredate, 'yyyy-mm-dd'))=12;
c curr%rowtype;
begin
open curr;
dbms_output.put_line('Employees with yearly incentive amounts:');
dbms_output.put_line('**********'); dbms_output.put_line('Employee ID Name of the
Employee Hire Date Incentive Amount');
dbms_output.put_line('**********');
loop fetch curr into c;
exit when curr%notfound;
work_exp:=MONTHS_BETWEEN(to_date('31/12/2020', 'dd/mm/yyyy'),c.emp_hiredate)/12;
case when work_exp>13 then incentive:=8000;
when work_exp>11 then incentive:=5000;
when work_exp>9 then incentive:=3000;
when work_exp>7 then incentive:=2000;
when work_exp>4 then incentive:=1000;
when work_exp>0 then incentive:=400;
else incentive:='null';
end case;
dbms_output.put_line(c.emp_id||' '||c.empname||' '||c.emp_hiredate||' '||
incentive);
end loop;
dbms_output.put_line('**********');
dbms_output.put_line('The number of rows fetched is '||curr%rowcount);
dbms_output.put_line('**********');
end;
/

exit;

Q1.
SET SERVEROUTPUT ON;
SET FEEDBACK OFF;
SET ECHO OFF;
SET HEADING OFF;
SET WRAP OFF;
SET LINESIZE 10000;
SET TAB OFF;
SET PAGES 0;
SET DEFINE OFF;

DECLARE
act NUMBER;
ter NUMBER;
superv VARCHAR2(200);

CURSOR curr IS
SELECT emp_id,
emp_fname || ' ' || emp_lname AS empname,
emp_status,
mgr_id
FROM emp
WHERE UPPER(emp_status) = 'ACTIVE';
CURSOR curr2 IS
SELECT emp_id,
emp_fname || ' ' || emp_lname AS mgrname,
emp_status AS stat,
mgr_id AS mgr_sup_id
FROM emp
WHERE UPPER(emp_status) = 'TERMINATED';

c1 curr%ROWTYPE;
c2 curr2%ROWTYPE;

BEGIN
SELECT COUNT(emp_id) INTO act FROM emp WHERE UPPER(emp_status) = 'ACTIVE';
SELECT COUNT(emp_id) INTO ter FROM emp WHERE UPPER(emp_status) = 'TERMINATED';

DBMS_OUTPUT.PUT_LINE('**Status Count**');
DBMS_OUTPUT.PUT_LINE('Active ' || act);
DBMS_OUTPUT.PUT_LINE('Terminated ' || ter);

DBMS_OUTPUT.PUT_LINE('**Employees under Terminated Manager**');


DBMS_OUTPUT.PUT_LINE('emp_id emp_name Mgr_name Mgr_Status Mgr_Supervisor');

OPEN curr;

LOOP
FETCH curr INTO c1;
EXIT WHEN curr%NOTFOUND;

OPEN curr2;
LOOP
FETCH curr2 INTO c2;
EXIT WHEN curr2%NOTFOUND;

IF c1.mgr_id = c2.emp_id THEN


-- Get the supervisor of the terminated manager
SELECT emp_fname || ' ' || emp_lname
INTO superv
FROM emp
WHERE emp_id = c2.mgr_sup_id;

DBMS_OUTPUT.PUT_LINE(c1.emp_id || ' ' || c1.empname || ' ' ||


c2.mgrname || ' ' || c2.stat || ' ' || superv);
END IF;
END LOOP;

CLOSE curr2;
END LOOP;

CLOSE curr;
END;
/
EXIT;

Q4 :

SET SERVEROUTPUT ON;

DECLARE
-- Cursor for active employees
CURSOR active_cur IS
SELECT NVL(e.dept_id, 0) AS department_id,
COUNT(e.emp_id) AS total_employees,
SUM(e.emp_sal) AS total_salary
FROM emp e
WHERE e.emp_status = 'Active'
GROUP BY NVL(e.dept_id, 0)
ORDER BY department_id;

-- Cursor for terminated employees


CURSOR terminated_cur IS
SELECT NVL(e.dept_id, 0) AS department_id,
COUNT(e.emp_id) AS total_employees,
SUM(e.emp_sal) AS total_salary
FROM emp e
WHERE e.emp_status = 'Terminated'
GROUP BY NVL(e.dept_id, 0)
ORDER BY department_id;

active_rec active_cur%ROWTYPE;
terminated_rec terminated_cur%ROWTYPE;

BEGIN
-- Print details of Active employees
DBMS_OUTPUT.PUT_LINE('Details of Active employees');
DBMS_OUTPUT.PUT_LINE('Department_id Total_employees Total_salary');

OPEN active_cur;
LOOP
FETCH active_cur INTO active_rec;
EXIT WHEN active_cur%NOTFOUND;

DBMS_OUTPUT.PUT_LINE(active_rec.department_id || ' ' ||


active_rec.total_employees || ' ' || active_rec.total_salary);
END LOOP;
CLOSE active_cur;

-- Print details of Terminated employees


DBMS_OUTPUT.PUT_LINE('Details of Terminated employees');
DBMS_OUTPUT.PUT_LINE('Department_id Total_employees Total_salary');

OPEN terminated_cur;
LOOP
FETCH terminated_cur INTO terminated_rec;
EXIT WHEN terminated_cur%NOTFOUND;

DBMS_OUTPUT.PUT_LINE(terminated_rec.department_id || ' ' ||


terminated_rec.total_employees || ' ' || terminated_rec.total_salary);
END LOOP;
CLOSE terminated_cur;
END;
/

EXIT;

You might also like