Présentation de symfony - Human talks aux docks le 8 juillet 2014
Corrig Projet P L S Q L
1. Corrigé du mini projet Pl/SQL
1 – Trigger
Dans la table Ord, le total correspond au cumul des totaux par ligne de
commande Itemtot.
create trigger tot
after Insert or update or delete
ON item
declare sumitem item.itemtot%type;
begin
for enr_rec in (select ordid from ord)
loop
select sum(itemtot) into sumitem from item
where item.ordid=enr_rec.ordid;
update ord
set ord.total= sumitem
where ord.ordid=enr_rec.ordid;
end loop;
end;
/
Dans la table Item, le prix appliqué actual price est supérieur ou égal au prix
minimum minprice en cour à la date de la commande
create trigger mini
before insert or update on item
declare
prixactuel item.actualprice%type;
prixmini price.minprice%type;
begin
for enr_rec in(select prodid from item)
loop
select actualPrice into prixactuel
from item
where prodid=enr_rec.prodid;
2. select minprice into prixmini
from price
where prodid=enr_rec.prodid and startdate<sysdate and (enddate is null
or enddate>sysdate);
if(prixactuel<prixmini)
then update item
set actualprice=prixmini
where prodid=enr_rec.prodid;
end if;
end loop;
end;
/
Dans la table customer, repid reference un employé commercial
Create or replace trigger comercial before insert or update on customer
declare
numemp emp.job%type;
test emp.job%type:='SALESMAN';
begin
for num in (select repid from customer)
loop
select job into numemp from emp where empno=num.repid;
if(sql%notfound)
then dbms_output.put_line('num d employer inxesistant: '||num.repid);
else if(numemp!=test)
then dbms_output.put_line('employer n est pas un
commercial:'||num.repid);
end if;
end if;
end loop;
end;
/
3. 2- Script qui affiche le total des ventes par produit et par commercial
vue utilisé : vue1 pour facilité la recherche des produit et des employé
create or replace view vue1 as
select C.repid,E.ename, I.prodid,P.descrip, I.Itemtot
from customer c, ord O, item I,emp E,product P
where C.custid=O.custid and O.ordid=I.ordid and C.repid=E.empno and
P.proid=I.prodid;
Script :
declare
blanc vue1.descrip%type:='--------------------';
Cursor lesEmp is select distinct repid from vue1;
Cursor lesProd is select distinct prodid from vue1;
nomEmp vue1.ename%type;
nomProd vue1.descrip%type;
valeur vue1.itemtot%type;
i number;
begin
dbms_output.put(blanc||' ');
for numEmp in lesEmp
loop
select distinct ename into nomEmp from vue1 where repid=numEmp.repid;
dbms_output.put(nomEmp);
end loop;
dbms_output.new_line();
for numProd in lesProd
loop
select distinct descrip into nomProd from vue1 where prodid=numProd.prodid ;
dbms_output.put(nomProd);
i:=20-length(nomProd);
if(i>0)
then for j in 1..i
loop dbms_output.put(' ');
end loop;
end if;
dbms_output.put(' ');
for numEmp in lesEmp
loop
5. 3. Créer une table de ce format et l’afficher (requête dynamique)
drop table resultat
/
Declare
Cursor curRepName is
Select Distinct RepName
From VENTE
Order By RepName;
Cursor curDescrip is
Select Distinct Descrip
From VENTE
Order By Descrip;
myRepName VENTE.RepName%TYPE;
myDescrip VENTE.Descrip%TYPE;
myAmount Number(8,2);
qryCreateTable LONG;
qryCreateRows LONG;
Begin
/* Debut de la requete pour creer la table */
qryCreateTable:='CREATE TABLE RESULTAT (Descrip Varchar2(30)';
/* Creation de la requete */
Open curRepName;
Loop
Fetch curRepName into myRepName;
Exit When curRepName%NOTFOUND;
qryCreateTable:=qryCreateTable || ',' || myRepName || '
number(8,2)';
End Loop;
Close curRepName;
/* Fin de la requete */
qryCreateTable:=qryCreateTable || ')';
/* On execute la requete */
execute immediate qryCreateTable;
dbms_output.put_line(qryCreateTable);
/* Creation des tuples */
6. Open curDescrip;
Loop
Fetch curDescrip into myDescrip;
Exit When curDescrip%NOTFOUND;
qryCreateRows:='insert into resultat (descrip) values ('||'''' ||
myDescrip ||''''||')';
execute immediate qryCreateRows;
--dbms_output.put_line(qryCreateRows);
End Loop;
Close curDescrip;
/* On complete maintenant les valeurs */
Open curRepName;
Loop
Fetch curRepName into myRepName;
Exit When curRepName%NOTFOUND;
Open curDescrip;
Loop
Fetch curDescrip into myDescrip;
Exit When curDescrip%NOTFOUND;
Select sum(itemtot)
Into myAmount
From VENTE
Where repname=myRepName
And descrip=myDescrip;
qryCreateRows:='update resultat set ' || myRepName || '='''
|| myAmount || ''' where descrip=' || '''' || myDescrip || '''';
--dbms_output.put_line(qryCreateRows);
execute immediate qryCreateRows;
End Loop;
Close curDescrip;
End Loop;
Close curRepName;
End;
/
select * from resultat;
7. Traces d’exécution :
SQL> set serveroutput on;
SQL> set echo off;
SQL> set verify on;
SQL> @6.sql
DOC>L'affichage se fait sous forme de tableau de contingence avec
DOC>le nom du produit en lignes et le nom du commercial en colonnes
Procédure PL/SQL terminée avec succès.
PRODDESCRIP MARTIN TURNER
------------------------------ ---------- ----------
ACE TENNIS RACKET I 896 1400
ACE TENNIS RACKET II 180 1800
…