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

--Correction TP2PLSQL

--PARTIE1
--1

CREATE OR REPLACE FUNCTION FN_NBR_DEPARTEMENT


return number
IS
total number;

begin
select count (*) into total from departments ;
return total;
END;
/

declare
nbre number;
begin
nbre := FNNBRDEPARTMENT;
dbms_output.put_line(nbre);
end;
/

--APPEL
--SELECT FN_NBREDEPARTEMENT FROM dual;

--2

CREATE OR REPLACE FUNCTION FN_NOMDEPT


(id employees.employee_id%type)
RETURN departments.Department_name%type
IS
nom departments.Department_name%type;
BEGIN
SELECT department_name
INTO nom
FROM employees E
JOIN departments D
ON E.department_id=D.department_id
where employee_id=id;
RETURN nom;
END;
/
--APPEL

Declare
nom departments.Department_name%type;
BEGIN
nom:=fn_nomdept(100);
dbms_output.put_line(nom);
END;
/

--3
CREATE OR REPLACE PROCEDURE proc_details_emp
IS
begin
for i in(select E.last_name nomE, E.first_name prenomE, M.last_name nomM,
M.first_name prenomM
from Employees E
Join Employees M
ON E.manager_id = M.employee_id)
loop
dbms_output.put_line(i.nomE||' '||i.prenomE||' manager : '||i.nomM||' '||
i.prenomM);
end loop;
end;
/

--APPEL

execute proc_details_emp;

--4

CREATE OR REPLACE PROCEDURE PROC_SALMOY


(a out integer)
IS
BEGIN
a:=0;
FOR i in (SELECT department_id id,
ROUND(AVG(salary)) Moyenne FROM employees GROUP BY department_id ) LOOP
a:=a+1;
dbms_output.put_line(i.id || ' '||i.moyenne);
END LOOP;
END;
/

--APPEL

DECLARE
a number;
BEGIN
PROC_SALMOY(a);
dbms_output.put_line(a);
END;
/
--5
--Creation

CREATE OR REPLACE FUNCTION fn_nbre_salarie


(id_dept employees.department_id%type)
RETURN number
IS
a number;
BEGIN
select count(*) into a from employees where department_id=id_dept;
return a;
END;
/
--APPEL
Select fn_nbre_salarie(&id) from dual;
--OU
declare
a number;
Begin
a:=fn_nbre_salarie(&id);
dbms_output.put_line(a);
end;
/

--Procédure équivalente :

CREATE OR REPLACE PROCEDURE proc_nbre_salarie


(id_dept in employees.department_id%type, c out number)
is
BEGIN
select count(*) into c from employees where department_id=id_dept;
END;
/

--APPEL
Declare
id number :=&id;
c number;
begin
Proc_nbre_salarie(id,c);
dbms_output.put_line(c);
end;
/
--6
CREATE OR REPLACE PROCEDURE PROC_TEST_NBR_salarie
IS
BEGIN
for i in (select department_name nom, department_id id from departments) Loop
if(fn_nbre_salarie(i.id)>40) then
dbms_output.put_line(i.nom);
end if;
end Loop;
END;
/

--APPEL

declare
a integer;
begin
PROC_TEST_NBR_salarie(a);
dbms_output.put_line(a);
end;
/

--7
CREATE OR REPLACE PROCEDURE PROC_SAL_SUP
(id employees.employee_id%type)
IS
BEGIN
for i in (select * from employees where salary > (select salary from employees
where employee_id=id)) loop
dbms_output.put_line(i.last_name);
end loop;
END;
/

--APPEL
begin
PROC_SAL_SUP(121);
end;
/

--8
CREATE OR REPLACE Function FN_Moy_salaire
(id employees.employee_id%type)
return number
IS
moyenne number;
BEGIN
select Round(avg(salary),2) into moyenne
from employees where department_id =(select department_id from employees where
employee_id= id);
Return moyenne;
end;
/

--9
CREATE OR REPLACE PROCEDURE Proc_list_emp
IS
Cursor c1 is select distinct E.manager_id id, M.last_name A, M.first_name B from
employees E join Employees M on E.manager_id=M.employee_id;
Cursor c2(idM employees.manager_id%type) is select last_name C, first_name D from
employees where manager_id=idM;
var_c1 c1%rowtype;
var_c2 c2%rowtype;
BEGIN
OPEN C1;
loop
Fetch c1 into var_c1;
exit when (c1%notfound);
dbms_output.put_line('**** Manager '||c1%rowcount|| ' : '||var_c1.A||' '||
var_c1.B);
open c2(var_c1.id);
loop
Fetch c2 into var_c2;
exit when c2%notfound;
dbms_output.put_line('********* '||var_c2.C||' '||var_c2.D);
end loop;
dbms_output.put_line('*** TOTAL : '||c2%rowcount);
close c2;
end loop;
dbms_output.put_line('*** TOTAL MANAGER : '||c1%rowcount);
close c1;
END;
/

--10
CREATE OR REPLACE FUNCTION fn_trimestre(d date)
return integer
IS
trimestre integer;
BEGIN
Select to_char(d,'q') into trimestre from dual;
return trimestre;
END;
/

--APPEL
select * from employees where fn_trimestre(hire_date)=2 and extract(year from
hire_date)=1998;
--PARTIE 2
--1
declare
man employees.first_name%type:='Payam';
n number ;
function fn_emp (manager employees.first_name%type)
return number is
nb number ;
begin
select count(*) into nb from employees where manager_id = ( select employee_id from
employees where first_name = manager );
return nb;
end ;
begin
n:= fn_emp(man);
dbms_output.put_line(n);
end ;
/

--2
declare
procedure proc_depts
is
begin
for i in (select department_name A, avg(salary) B
from departments d
join employees e
on e.department_id=d.department_id
group by department_name
having count(salary)>20) loop
dbms_output.put_line(i.a||' '||i.b);
end loop;
end;
begin
proc_depts;
end;
/

You might also like