forked from srikanthpragada/plsql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdept_hod_city_noemployees.sql
52 lines (46 loc) · 1.24 KB
/
dept_hod_city_noemployees.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
set serveroutput on
declare
cursor deptcur is
select * from departments
where manager_id is not null;
function employees_count(p_deptid number)
return number
is
v_count number(3);
begin
select count(*) into v_count
from employees
where department_id = p_deptid;
return v_count;
end;
function hod_name(p_deptid number)
return varchar2
is
v_name employees.first_name%type;
begin
select first_name into v_name
from employees
where employee_id =
(select manager_id from departments where department_id = p_deptid);
return v_name;
end;
function dept_city(p_deptid number)
return varchar2
is
v_city locations.city%type;
begin
select city into v_city
from locations
where location_id =
(select location_id from departments where department_id = p_deptid);
return v_city;
end;
begin
for deptrec in deptcur
loop
dbms_output.put_line( rpad(deptrec.department_name,20) ||
rpad(hod_name(deptrec.department_id),20) ||
rpad(dept_city(deptrec.department_id),20) ||
employees_count(deptrec.department_id));
end loop;
end;