Вы находитесь на странице: 1из 5

--2

begin
execute immediate 'create table info_client(id_client number(2), nume_client varchar2(20),
numar_comenzi number(2))';
end;
/
begin
execute immediate 'create table info11 ' ||
'(id_client number(2), ' ||
'nume_client varchar2(29) ' ||
')' ;
end;
/
--1
declare
cat produse.categorie%type;
pr rand_comenzi.pret%type;
id_prod rand_comenzi.id_produs%type;
begin
update rand_comenzi
set pret=pret-1
where id_produs in(select r.id_produs
from produse p, rand_comenzi r
where p.id_produs=r.id_produs and p.categorie='&p');
end;
/
--3
create or replace function fct_numara_comenzi
(p_id number) return number is
total number;
exc exception;
begin
select count(nr_comanda) into total
from comenzi where id_client=p_id;
if total=0 then raise no_data_found;
else return total;
if sql%rowcount=0 then raise exc;
end if;
end if;
exception
when no_data_found then return 0;
when exc then return 0;
end;

--3
create or replace procedure proc_afiseaza_dep1
is
cursor c1 is
select id_departament, denumire_departament, fct_numara_ang(id_departament) as nr_angajati
from departamente
order by nr_angajati desc;
begin
for i in c1 loop
dbms_output.put_line(i.id_departament|| ' '|| i.denumire_departament||' '||i.nr_angajati);
exit when c1%rowcount=3;
end loop;
end;
/
set serveroutput on;
execute proc_afiseaza_dep1;

-- ex 3 primele 3 departamente cu cei mai multi angajati cu salariul mai mare decat media; in procedura

set serveroutput on;

create or replace procedure top_three_departments is

cursor final_deps is
select a.id_departament, d.denumire_departament, a.numar
from
(
select distinct id_departament, fct_numara_ang(id_departament) as numar
from angajati
where id_departament is not null
order by 2 desc
)a
join departamente d on a.id_departament = d.id_departament
where rownum <= 3;

begin

-- toate departamentele si salariile lor asociate


-- ordonez dupa nr angati
-- iau primele 3 departamente

for i in final_deps loop


dbms_output.put_line('Departament: ' || i.id_departament || ' Nume: ' ||
i.denumire_departament || ' Numar angajati: ' || i.numar);
end loop;

end;
--2
set serveroutput on;
create or replace function fct_numara_ang(id_dep number) return number as
sal_mediu number;
no_department_found exception;
no_employees_found exception;
v_nr number;
exista_angajati number;
exista_departament number;
begin
select
case when count(1) = 0 then 0 else 1 end
into exista_departament
from departamente
where id_departament = id_dep;

--dbms_output.put_line('Departamentul ' || id_dep || ' exista? : ' || exista_departament);

if (exista_departament = 0) then
raise no_department_found;
end if;

select
case when count(1) = 0 then 0 else 1 end
into exista_angajati
from angajati
where id_departament=id_dep;

--dbms_output.put_line('Departamentul ' || id_dep || ' are angajati? : ' || exista_angajati);

if (exista_angajati = 0) then
raise no_employees_found;
end if;

select
avg(salariul)
into sal_mediu
from angajati;

--dbms_output.put_line('Salariul mediu al angajatilor este ' || sal_mediu);

select
count(id_angajat)
into v_nr
from angajati
where
id_departament = id_dep and salariul > sal_mediu;

--dbms_output.put_line('Numarul angajatilor este ' || v_nr);

return v_nr;

exception
when no_department_found then return -1;
when no_employees_found then return -2;
end;

--sa se afiseze pt fiecare comanda incheiata dupa 1 ian 1998 nr comenzii, data incheierii acesteia si lista
tuturor produselor
--incluse in comanda alaturi de cantitatea comandata
declare
cursor c1 is
select nr_comanda, data
from comenzi where nr_comanda in
(select nr_comanda from rand_comenzi where data>to_date('01-JAN-1998','DD-MON-YYYY'));
cursor c2(p_id comenzi.nr_comanda%type) is
select p.denumire_produs, r.cantitate
from produse p, rand_comenzi r
where p.id_produs=r.id_produs and r.nr_comanda=p_id;
begin
for i in c1 loop
dbms_output.put_line(i.nr_comanda||' '||i.data);
for j in c2(i.nr_comanda) loop
dbms_output.put_line(j.denumire_produs||' '||j.cantitate);
end loop;
end loop;
end;
/
--sa se construiasca un bloc prin care sa se dubleze salariul angajatilor care lucreaza in departamentul cu
denumire 'executive'
--in cazul in care exista angajati in dep respectiv, se va afisa nr de modificari facute iar daca nu, se va
invoca o exceptie
declare
begin
update angajati
set salariul=salariul*2 where id_departament in(select id_departament from departamente where
denumire_departament='Executive');
if sql%found then dbms_output.put_line(sql%rowcount);
else raise_application_error(-200001,'error');
end if;
end;
/
--sa se scada cu 5% limita de credit a clientilor care au incheiat comenzi in anul a carui valoare e data de
la tastatura
--in caz de succes, afisati nr de inreg modificate, altfel invocam exceptie
begin
update clienti
set limita_credit=limita_credit-(limita_credit*0.5)
where id_client in (select id_client from comenzi where extract(year from data)='&a');
if sql%found then dbms_output.put_line(sql%rowcount);
else raise_application_error(-20002, 'error');
end if;
end;
/

Вам также может понравиться