0% found this document useful (0 votes)
39 views5 pages

TP 1

This document contains examples of PL/SQL code snippets using various programming concepts like variables, conditions, loops, cursors, and stored procedures. The snippets demonstrate how to perform operations on database tables like retrieving, inserting, updating, and deleting data.

Uploaded by

mehdi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views5 pages

TP 1

This document contains examples of PL/SQL code snippets using various programming concepts like variables, conditions, loops, cursors, and stored procedures. The snippets demonstrate how to perform operations on database tables like retrieving, inserting, updating, and deleting data.

Uploaded by

mehdi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

PARTIE 1

--1
--a
/*
DECLARE
d date;
BEGIN
select sysdate
into d
from dual;
dbms_output.put_line('la date d''aujourd''hui : '||d);
END;
/
*/
--b
/*
DECLARE
a number :=44.5;
b a%type :=61.8;
BEGIN
dbms_output.put_line(a||'+'||b||'='||(a+b));
END;
/
*/

--c
--avec FOR
/*
BEGIN
dbms_output.put_line('LISTE1 :');
for i in 1..10 LOOP
if i=10 then
dbms_output.put(i);
else
dbms_output.put(i||',');
end if;
end loop;
dbms_output.new_line();
dbms_output.put_line('LISTE2 :');
FOR i in reverse 1..100 LOOP
if i mod 2 = 0 then
dbms_output.put(i||',');
end if;
end loop;
dbms_output.put_line(0);
end;

*/

--d
/*
DECLARE
A integer := &A;
B integer := &B;
BEGIN
dbms_output.put_line(A||'+'||B||'='||(A+B));
dbms_output.put_line(A||'*'||B||'='||(A*B));
END;
/
*/

--e
/*
DECLARE
A integer := &A;
B integer := &B;
C integer := &C;
BEGIN
if (A<=B and B<=C) then
dbms_output.put_line('A<B<C');
elsif (A<=C and C<=B) then
dbms_output.put_line('A<C<B');
elsif (B<=A and A<=C) then
dbms_output.put_line('B<A<C');
elsif (C<=A and A<=B) then
dbms_output.put_line('C<A<B');
elsif (B<=C and C<=A) then
dbms_output.put_line('B<C<A');
else
dbms_output.put_line('C<B<A');
end if;
end;
/
*/

--f
/*
DECLARE
A integer :=&A;
f integer :=1;
BEGIN
FOR i in 2..A loop
f := f * i;
end loop;
Dbms_output.put_line(A||'!='||f);
end;
/
*/

--2
--a
/*
DECLARE
nom employees.last_name%type;
BEGIN
select last_name into nom from employees where employee_id=110;
dbms_output.put_line(nom);
END;
/
*/

--b
/*
DECLARE
a number;
BEGIN
select count(*) into a from employees where department_id=50;
dbms_output.put_line('le nombre des employés du département 50 est '||a);
END;
/
*/

--c
/*
DECLARE
sal_min employees.salary%type;
sal_max employees.salary%type;
BEGIN
SELECT min(salary), max(salary) into sal_min, sal_max from employees where
department_id=50;
dbms_output.put_line('le salaire min du département 50 est '||sal_min||chr(10)||'le
salaire max du département 50 est '||sal_max);
END;
/
*/

PARTIE 2
--1
--a
--i Curseur IMPLICITE
/*
BEGIN
FOR i in (select E.department_id id, department_name nom, Round(AVG(salary))
moyenne
FROM employees E
JOIN departments D
ON E.department_id=D.department_id
GROUP BY E.department_id, department_name)
LOOP
dbms_output.put_line(i.id||' '||i.nom||' '||i.moyenne);
END LOOP;
END;
/
*/

--ii Curseur EXPLICITE


/*
DECLARE
CURSOR C is select E.department_id id, department_name nom, Round(AVG(salary))
moyenne
FROM employees E
JOIN departments D
ON E.department_id=D.department_id
GROUP BY E.department_id, department_name;
var_C C%rowtype;
BEGIN
OPEN C;
LOOP
FETCH C into var_C;
EXIT WHEN C%notfound;
dbms_output.put_line(var_C.id||' '||var_C.nom||' '||var_C.moyenne);
END LOOP;
CLOSE C;
END;
/
*/
b-
set serveroutput on;
Begin
for i in (select count(*) as nb, department_id from employees group by
department_id)
loop
dbms_output.put_line(i.department_id ||' '||i.nb);
end loop;
end;
/
c-
/
Begin
for i in (select first_name, last_name from employees where department_id in(select
count(*),department_id from employees group by department_id having count(*)
>20) );
loop
dbms_output.put_line(i.first_name ||' '||i.last_name);
end loop;
end;
/
d-
/

Declare
cursor first is select disctinct(e.fisrt_name) a, e.last_name b, m.manager_id c
from employees e join employees m on m.manager_id = e.employee_id
cursor second (id employees.manager_id%TYPE) is select first_name as d, last_name e
from employees where manager_id = id
VAR_FIRST first%ROWTYPE;
VAR_SECOND second%ROWTYPE;
Begin
open first;
loop
fetch first into VAR_FRIST;
exit when first%NOTFOUND;
dbms_output.put_line('Manager : '||VAR_FIRST.a||' '||VAR_FIRST.b);
open second(VAR_FIRST.ma);
loop
fetch second into VAR_SECOND;
exit when second%NOTFOUND;
dbms_output.put_line('Employees : '||VAR_SECOND.d ||' '||VAR_SECOND.e);
end loop;
close first
end loop;
close first;
end
/
2/
DECLARE
vsql varchar(250) := 'create table produits(
id_produit int prilary key,
nomproduit varchar(30),
categoriep varchar(30),
prixproduit numeric
)';;
Begin
execute immediate(vsql);
end;
/

You might also like