Sunteți pe pagina 1din 36

Ministerul Educaiei al Republicii Moldova

Universitatea Tehnic a Moldovei


Facultatea Calculatoare, Informatic i Microelectronic
Catedra Automatic i Tehnologii Informaionale

Raport
Lucrare de laborator nr. 8
la disciplina: Baze de date i cunotine

tema: Proceduri stocate i funcii definite de utilizator

A verificat: lector superior Bagrin Veronica A


efectuat: st. gr. TI-132 Spatari Artur

Chiinu 2016

Coninut

Sarcina 1................................................................................................................................................
Interogri simple...............................................................................................................................
Interogri multi-relaie....................................................................................................................10
Sarcina 2..............................................................................................................................................17
Sarcina 3..............................................................................................................................................17
Sarcina 4..............................................................................................................................................18
Sarcina 5..............................................................................................................................................18
Sarcina 6..............................................................................................................................................20
Interogri simple.............................................................................................................................20
Interogri multi-relaie....................................................................................................................24
Sarcina 7..............................................................................................................................................32
Sarcina 8..............................................................................................................................................32
Sarcina 9..............................................................................................................................................32
Sarcina 10............................................................................................................................................32
Sarcina 11............................................................................................................................................34
Concluzii..............................................................................................................................................36

Sarcina lucrrii
2

1. S se creeze proceduri stocate n baza exerciiilor din capitolul 4. Parametrii de intrare trebuie
s corespund criteriilor din clauzele WHERE ale exerciiilor respective.
2. S se creeze procedura stocat care ar elimina toate produsele unui productor sau unul din
tipurile de produse ale acestui productor. n calitate de parametru de intrare, s se ia numele
productorului i tipul produsului.
3. S se creeze o procedur stocat care ar insera n baza de date un model nou de imprimant. n
cazul n care datele inserate sunt incorecte sau incomplete, s se afieze un mesaj de avertizare.
n calitate de parametri de intrare apar datele pentru modelul respectiv.
4. S se creeze o procedur stocat care, n calitate de parametru de intrare, s aib numrul de
tipuri de produs fabricate de ctre productor. n urma executrii procedurii, trebuie s se
afieze un mesaj informativ, care s includ valoarea parametrului inserat i un tabel cu
coloanele Producator, Produs, Nr_De_Model pentru fiecare tip de produs.
5. S se creeze o procedur stocat care ar calcula preul ce trebuie s-l achite cumprtorul n
timpul promoiilor speciale la magazinul de calculatoare. Procedura va trebui s calculeze
preul nou pentru toate produsele unui productor anumit, n cazul cnd se anun promo ia. Un
cumprtor poate procura deodat un singur model de produse, dar mai multe uniti de acest
produs. Reducerea poate fi de cteva tipuri n funcie de tipul produsului, pre ul ini ial i
numrul de uniti de marf procurat.
n calitate de parametri de intrare, se vor lua:
-

modelul produsului procurat de cumprtor;

numrul de uniti de marf procurat;

procentul de reducere la pre n funcie de numrul de uniti de marf procurat;

procentul de reducere la pre n dependen de tipul de marf procurat;

procentul de reducere la pre n dependen de preul produsului fr reducere.

Astfel, trebuie s se afieze Modelul, Tipul produsului, Productorul, Preul fr reducere


i Suma total spre achitare. Suma total spre achitare trebuie s in cont de reducerile
calculate. Implicit asupra produselor procurate nu trebuie s influeneze nici o reducere.
6. S se creeze funcii definite de utilizator n baza exerciiilor din capitolul 4. Parametrii de
intrare
3

trebuie s corespund criteriilor din clauzele WHERE ale exerciiilor respective.


7. S se scrie funcia care ar calcula diferena aritmetic dintre valoarea curent a nregistrrii n
cmpul Pre al tabelului pc_uri i valoare cmpului precedent. Ordonarea nregistrrilor n
tabel trebuie s fie fcut dup cmpul Cod.
n calitate de exemplu al utilizrii aceste funcii poate servi cmpul diferena.
8. Folosind tabelul laptop_uri, s se creeze o funcie definit de utilizator, care ar citi numerele
cmpului i ar ataa sufixul respectiv la valoarea necesar. De exemplu, n cazul n care cmpul
Pret, se adaug sufixul $, iar dac cmpul este Viteza, se ataeaz sufixul GHz etc.
S se analizeze exemplul de mai jos n calitate de model:
Cod Model Viteza
Ram
Hd
Pret
Ecran
1
1298 3.5 GHz 32 GB 4 GB 700.00$ 11
2
1321 5.0 GHz 64 GB 8 GB 970.00$ 12
Formatul funciei este urmtor: nume_funcie
(nume_cmp)

9. Se dorete realizarea unei funcii definite de utilizator din baza de date calculatoare, care ar
putea transforma cmpul Pret din dolari (stare curent) n euro sau n lei. Se utilizeaz tabelul
imprimante. Formatul funciei este urmtorul:
nume_funcie (valoare_pret, curs_BNM, valuta)

Valoare

afiat trebuie s fie de tip MONEY.


10.

S se creeze o funcie definit de utilizator, care ar returna toate datele de un anumit

produs din stoc. Se definete urmtorul format al funciei:


nume_funcie (pret_minimal, pret_maximal, tip_produs)

unde parametrii pot lua valori


-

tip_produs

- pc, laptop sau imprimanta;

pret_minimal

pret_maximal

- 0.00, valoarea implicit;


- 10000.00, valoarea implicit.

11.

Se cere realizarea unei funcii definite de utilizator, care ar calcula cel mai ieftin sau cel

mai scump produs al unui productor. Productorul este luat n calitate de parametru. Formatul
funciei este urmtorul:
nume_funcie (nume_producator, flag)

unde, dac flag=1, va afia preul minimal pentru utilizatorul dat, iar dac flag=0 va afia
preul maximal.
Un exemplu de rezultate returnate de funcie:
Productor Nume_Produs
C
Laptop_uri

Pret
970

Sarcina 1
Interogri simple
1. S se gseasc modelul, viteza procesorului i capacitatea discului dur pentru toate pc-urile care
cost mai puin de 500 $. Schema rezultatului afiat va fi Model, Viteza i HD.
use DBlab8
go
if OBJECT_ID('P1', 'P') is not null drop procedure P1 go
create procedure P1
@Pret float
as
Select Model, Viteza, HD from pc_uri where Pret<@Pret
exec P1
@Pret=500

2. S se gseasc productorii de imprimante. Afiai: Producator.


use DBlab8
go
if OBJECT_ID('P2', 'P') is not null drop procedure P2 go
create procedure P2 @Tip
varchar(10)
as
select Producator from produse where Tip=@Tip group by Producator
exec P2 @Tip='Imprimante'

3. S se gseasc modelul, capacitatea de memorie operativ i dimensiunea ecranului fiecrui


laptop cu preul mai mare de 1000 $.
use DBlab8
go
if OBJECT_ID('P3', 'P') is not null drop procedure P3 go
create procedure P3
@Pret float as
select Model, RAM, Ecran from laptop_uri where Pret>@Pret;
exec P3
@Pret=1000

4. S se gseasc toate liniile tabelului imprimante pentru imprimantele color.


use DBlab8
go
if OBJECT_ID('P4', 'P') is not null drop procedure P4 go
create procedure P4
@Color char(2)
as
select * from imprimante where Color=@Color;
exec P4 'da'

5. S se gseasc modelul i viteza procesorului i capacitatea discului dur ale pc-urilor mai
ieftine de 600 $ i care posed cd-rom-urilor cu viteze 12x i 24x.
6

use DBlab8
go
if OBJECT_ID('P5', 'P') is not null drop procedure P5 go
create procedure P5 @Pret
float,
@CD1 varchar(3),
@CD2 varchar(3)
as
select Model, Viteza, HD from pc_uri where
pret<@Pret and (cd=@CD1 or cd=@CD2);
exec P5 600, '12x', '24x'

6. S se afieze productorii i viteza pc-urilor ce posed HD-uri cu capacitatea cea mai mic.
use DBlab8
go
if OBJECT_ID('P6', 'P') is not null drop procedure P6 go
create procedure P6 @minHD
decimal(2,0)
as
select producator, viteza
from
produse inner join pc_uri
on produse.model=pc_uri.model and hd=@minHD
declare @min decimal(2,0)= (Select MIN(HD) from pc_uri) exec
P6 @min

7. S se gseasc modelele i productorii produselor cu preul mai mare de 450 $.


use DBlab8
go
if OBJECT_ID('P7', 'P') is not null drop procedure P7 go
create procedure P7
@Pret float as
select distinct l.Producator, l.Model from
produse as l left join pc_uri as p on
l.Model=p.Model left join laptop_uri as k on
l.Model=k.Model left join imprimante as i on
l.Model=i.Model
where p.Pret>@Pret or k.Pret>@Pret or i.Pret>@Pret;
exec P7 450

8. S se gseasc productorii care vnd laptopuri sau imprimante, dar nu vnd pc-uri.
use DBlab8
go
if OBJECT_ID('P8', 'P') is not null drop procedure P8 go
create procedure P8
@Tip varchar(10) as
select distinct Producator
from produse
where Producator not in
(select Producator from produse where Tip=@Tip);
exec P8 'PC'

9. S se gseasc productorii de pc-uri cu procesoare ce funcioneaz ca frecven a de tact nu mai


mic de 450 MHz. Afiai cmpul Productor.
7

use DBlab8
go
if OBJECT_ID('P9', 'P') is not null drop procedure P9 go
create procedure P9 @Viteza
decimal(3,0)
as
select distinct Producator
from produse as l inner join pc_uri as p on
l.Model=p.Model and Viteza>@Viteza;
exec P9 450

10.

S se gseasc imprimantele cu cel mai nalt pre. S se afieze Model, Pre.

use DBlab8
go
if OBJECT_ID('P10', 'P') is not null drop procedure P10 go
create procedure P10
as
select Model, Pret from imprimante
where Pret=(Select MAX(Pret) from imprimante)
exec P10

11.

S se gseasc viteza medie a pc-urilor.

use DBlab8
go
if OBJECT_ID('P11', 'P') is not null drop procedure P11 go
create procedure P11
as
select avg(Viteza) as VitezaMedie from pc_uri
exec
P11

12.

S se gseasc viteza medie a laptopurilor, al cror pre depete 1000 $.

use DBlab8
go
if OBJECT_ID('P12', 'P') is not null drop procedure P12 go
create procedure P12
@Pret float
as
select AVG(Viteza) as VitezaMedie from laptop_uri where
Pret>@Pret;
exec P12
1000

13.

S se gseasc viteza medie a pc-urilor produse de compania A.

use DBlab8
go
if OBJECT_ID('P13', 'P') is not null drop procedure P13 go
create procedure P13
@Producator char(1)
as
select distinct avg(Viteza) as VitezaMedie
from
pc_uri as l inner join produse as p
on l.Model= p.Model and Producator= @Producator;

exec P13 'A'

14.

S se gseasc modelele i productorii pc-urilor care au o vitez mai mare dect media

ram-ului laptopurilor.
use DBlab8
go
if OBJECT_ID('P14', 'P') is not null drop procedure P14 go
create procedure P14
as
select l.Model, l.Producator, p.Viteza
from produse
as l inner join pc_uri as p
on l.Model=p.Model and Viteza>(Select MAX(Viteza) from laptop_uri)
exec
P14

15.

Afiarea tuturor productorilor pc-urilor ce au ram-ul mai mare dect preul dublu al

celui mai ieftin pc. S se afieze modelul i productorul pc-urilor.


use DBlab8
go
if OBJECT_ID('P15', 'P') is not null drop procedure P15 go
create procedure P15
as
Select distinct p.Producator, p.Model
from pc_uri as
l inner join produse as p
on l.Model=p.Model and RAM>(Select AVG(RAM) from laptop_uri);
exec P15

16.
S se gseasc modelele laptopurilor cu preul mai mare dect preul dublu al celui mai
ieftin pc.
S se afieze modelul laptopului i preul acestuia.
use DBlab8
go
if OBJECT_ID('P16', 'P') is not null drop procedure P16 go
create procedure P16
as
select Model, Pret from laptop_uri
where Pret>(Select MIN(Pret) from pc_uri)*2;
exec
P16

17.

Pentru fiecare valoare a vitezei, s se gseasc preul mediu al pc-urilor. S se afi eze

Viteza i Pret_Mediu.
use DBlab8
go
if OBJECT_ID('P17', 'P') is not null drop procedure P17 go
create procedure P17
as
select Viteza, AVG(Pret)as PretMediu from pc_uri

group by Viteza;
exec
P17

Interogri multi-relaie
1. S se gseasc capacitile discurilor dure, care coincid cu 2 sau mai multe pc-uri. S se afi eze
HD.
use DBlab8
go
if OBJECT_ID('M1', 'P') is not null drop procedure M1 go
create procedure M1
as
select HD from pc_uri
group by HD having count(HD)>1 ;
exec
M1

2. S se gseasc perechile de modele de pc-uri, care posed procesoare cu aceia i vitez i


memorie ram cu aceiai capacitate. Ca rezultat, fiecare pereche se afieaz o singur dat, adic
(i,j), dar nu (i,j). Ordinea de afiare: model cu numr mai mare, modelul cu numr mai mic,
viteza i Ram.
use DBlab8
go
if OBJECT_ID('M2', 'P') is not null drop procedure M2 go
create procedure M2
as
select distinct p.Model, l.Model, l.Viteza, l.RAM
from pc_uri as p, pc_uri as l
where p.Viteza=l.Viteza and p.RAM=l.RAM
and p.Model>l.Model
order by p.Model ;
exec M2

3. S se gseasc laptopurile, a cror vitez este mai mic dect la orice pc. S se afi eze: Tip,
Model, Viteza.
use DBlab8
go
if OBJECT_ID('M3', 'P') is not null drop procedure M3 go
create procedure M3
as
select p.Tip, p.Model, l.Viteza
from laptop_uri as l inner join produse as p
on l.Model=p.Model and l.Viteza<(select MIN(Viteza) from pc_uri);
exec
M3

4. S se gseasc productorii celor mai scumpe laptopuri sau pc-uri. Afiai Producator, Model i
Pre.
10

use DBlab8
go
if OBJECT_ID('M4', 'P') is not null drop procedure M4 go
create procedure M4
as
select produse.Producator, produse.Model, Pret from produse, pc_uri where
produse.Model=pc_uri.Model and Pret=(Select max(Pret) from pc_uri) union
select produse.Producator, produse.Model, Pret from produse, laptop_uri
where produse.Model=laptop_uri.Model and Pret=(Select max(Pret) from laptop_uri);
exec
M4

5. S se gseasc dimensiunea medie a ecranului laptopurilor produse de fiecare productor. S se


afieze Producator i dimensiunea medie a ecranului.
use DBlab8
go
if OBJECT_ID('M5', 'P') is not null drop procedure M5 go
create procedure M5
as
select produse.Producator, avg(laptop_uri.Ecran) as DimensiuneEcran from
produse , laptop_uri where produse.Model=laptop_uri.Model group by
produse.Producator
exec
M5

6. S se gseasc productorii, care produc cel puin 3 pc-uri. Afiai Producator, numrul de
modele.
use DBlab8
go
if OBJECT_ID('M6', 'P') is not null drop procedure M6 go
create procedure M6
@pcNumber int
as
select produse.Producator, count(*) as NumarPC
from
pc_uri inner join produse
on pc_uri.Model=produse.Model
group by Producator having count(*)>=@pcNumber
exec M6
3

7. S se gseasc preul maximal al pc-urilor propuse de fiecare productor. Afiai Producator,


pre maxim.
use DBlab8
go
if OBJECT_ID('M7', 'P') is not null drop procedure M7 go
create procedure M7
as
select produse.Producator, max(pc_uri.Pret) as PretMaxim from
produse, pc_uri
where produse.Model=pc_uri.Model group by Producator

11

exec
M7

8. Pentru fiecare valoare a vitezei pc-urilor care depete 600 Mh, s se determine pre ul mediu
al calculatorului cu aceeai vitez. Afiai Viteza, pre mediu.
use DBlab8
go
if OBJECT_ID('M8', 'P') is not null drop procedure M8 go
create procedure M8 @Viteza
decimal(3,0)
as
select Viteza, avg(Pret) as PretMediu
from pc_uri
group by Viteza having Viteza>@Viteza;
exec M8
600

9. Afiarea tuturor productorilor care produc pc-uri i laptopuri care dispun de o vitez nu mai
mic de 750 Mh. Afiai Producator.
use DBlab8
go
if OBJECT_ID('M9', 'P') is not null drop procedure M9 go
create procedure M9 @Viteza
decimal(3,0),
@Tip1 varchar(10),
@Tip2 varchar(10) as
select distinct produse.Producator
from produse, pc_uri, laptop_uri
where
produse.Model=pc_uri.Model
and
(Tip=@Tip1 or Tip=@Tip2)
and
pc_uri.Viteza>=@Viteza
or
produse.Model=laptop_uri.Model
and laptop_uri.Viteza>=@Viteza;
exec M9 750,'PC', 'Laptop_uri'

10. S se gseasc suma total a preurilor celor mai ieftine imrpimante de fiecare tip.
use DBlab8
go
if OBJECT_ID('M10', 'P') is not null drop procedure M10 go
create procedure M10 @Tip1
varchar(10),
@Tip2 varchar(10),
@Tip3 varchar(10)
as
declare @a int, @b int, @c int
set @a=(select min(Pret) from imprimante where Tip=@Tip1) set
@b=(select min(Pret) from imprimante where Tip=@Tip2) set
@c=(select min(Pret) from imprimante where Tip=@Tip3) select
distinct @a+@b+@c as PretTotal from imprimante
exec M10 'Jet', 'Matrix','Laser'

12

11. S se gseasc modelul produsului cu cel mai mare pre. Se afieaz Model.
use DBlab8
go
if OBJECT_ID('M11', 'P') is not null drop procedure M11 go
create procedure M11
as
if ((select max(imprimante.Pret) from imprimante) > (select max(laptop_uri.Pret) from laptop_uri))
goto et1 else goto et2
et1:
if ((select max(imprimante.Pret) from imprimante) > (select max(pc_uri.Pret) from pc_uri))
select imprimante.Model from imprimante where imprimante.Pret = (select
max(imprimante.Pret) from imprimante) else select pc_uri.Model from pc_uri where pc_uri.Pret =
(select max(pc_uri.Pret) from pc_uri) return et2:
if ((select max(laptop_uri.Pret) from laptop_uri) > (select max(pc_uri.Pret) from pc_uri))
select
laptop_uri.Model from laptop_uri where laptop_uri.Pret = (select max(laptop_uri.Pret) from
laptop_uri) else select pc_uri.Model from pc_uri where pc_uri.Pret= (select max(pc_uri.Pret) from
pc_uri) return exec M11

12. S se gseasc productorii de imprimante care produc i pc-uri cu cel mai mic volum ram. S
se afieze Producator.
use DBlab8
go
if OBJECT_ID('M12', 'P') is not null drop procedure M12 go
create procedure M12 @Tip1
varchar(10),
@Tip2 varchar(10)
as
select distinct produse.Producator from
produse,
pc_uri
where
produse.Model=pc_uri.Model
and
(Tip=@Tip1 or Tip=@Tip2)
and pc_uri.RAM=(select min(RAM) from pc_uri)
exec M12 'PC', 'Imprimante'

13. S se gseasc preul mediu al pc-urilor i laptopurilor produse de ctre A. S se afi eze
Pre_med_comun.
use DBlab8
go
if OBJECT_ID('M13', 'P') is not null drop procedure M13 go
create procedure M13
@Producator char(1)
as
select avg(Tabel.Pret) as PretMediu from (
select sum(pc_uri.Pret) as Pret
from produse,pc_uri
where produse.Model = pc_uri.Model and produse.Producator = @Producator union
select sum(laptop_uri.Pret) as Pret
from produse,laptop_uri
where produse.Model = laptop_uri.Model and produse.Producator = @Producator ) as
Tabel exec
M13 'A'

13

14. S se gseasc preul mediu al pc-urilor pentru fiecare din productori care produc i
imprimante.
Afiai Producator, Pret_med_prod.
use DBlab8
go
if OBJECT_ID('M14', 'P') is not null drop procedure M14 go
create procedure M14 @Tip1
varchar(10),
@Tip2 varchar(10)
as
Select Producator, avg(pc_uri.Pret) as PretMediu from
produse,pc_uri
where produse.Model=pc_uri.Model and Producator in
(select distinct Producator from
produse as p
where Tip=@Tip1 and exists (select Producator from produse
where Tip=@Tip2 and Producator=p.Producator)) group by Producator
exec M14 'PC','imprimante'

15. S se gseasc capacitatea medie a HD pc-urilor ale acelor productori care produc i
imprimante. Afiai capacitatea medie HD.
use DBlab8
go
if OBJECT_ID('M15', 'P') is not null drop procedure M15 go
create procedure M15 @Tip1
varchar(10),
@Tip2 varchar(10)
as
Select Producator, avg(pc_uri.HD) as HDMediu
from produse,pc_uri
where produse.Model=pc_uri.Model and Producator in
(select distinct Producator from
produse as p
where Tip= @Tip1 and exists (select Producator from produse
where Tip=@Tip2 and Producator=p.Producator)) group by Producator
exec M15 'PC','imprimante'

16. S se gseasc productorii care produc numai un singur tip de produs. S se afi eze
productorul, tipul produsului, modelul i preul modelului respectiv.
use DBlab8
go
if OBJECT_ID('M16', 'P') is not null drop procedure M16 go
create procedure M16
as
Select p.Producator, produse.Model, m.Pret
from (Select Producator
from produse
group by Producator having count(Producator)=1 ) as p inner join
produse
on p.Producator=produse.Producator

14

inner join
(select pc_uri.Model, Pret from pc_uri
union
select laptop_uri.Model, Pret from laptop_uri
union
select imprimante.Model, Pret from imprimante) as m on
produse.Model=m.Model
exec
M16

17. S se analizeze preurile laptop-urilor i n funcie de valoare s fie grupate n 3 clase. Dac este
mai ieftin de 750 $- clasa econom., dac preul nu depete 1100 $- clasa business, dac pre ul
este mai mare de 1100 $- clasa premium. S se afieze tipul produsului, Mode, Pret i clasa
produsului.
use DBlab8
go
if OBJECT_ID('M17', 'P') is not null drop procedure M17 go
create procedure M17 @ClasaProdus1
varchar(10),
@ClasaProdus2 varchar(10),
@ClasaProdus3 varchar(10)
as
select produse.Tip, laptop_uri.Model, laptop_uri.Pret,ProductClass.ClasaProdus from
laptop_uri
inner join BazaDeDateLab3.dbo.ProductClass on (laptop_uri.Pret<750 and
ClasaProdus=@ClasaProdus1)
or ((laptop_uri.Pret>750 and laptop_uri.Pret<1100) and ClasaProdus=@ClasaProdus2)
or (laptop_uri.Pret>1100 and ClasaProdus=@ClasaProdus3)
inner join
produse on produse.Model=laptop_uri.Model
exec M17 'econom', 'business', 'premium'

18. n tabelul produse s se gseasc modelele la care prima cifr din model este o cifr impar, iar
ultima par i prima cifr este mai mic dect ultima. S se afi eze modelul, tipul produsului,
produsul primei i ultimei cifre din numrul modelului.
use DBlab8
go
if OBJECT_ID('M18', 'P') is not null drop procedure M18 go
create procedure M18
as
select model,
(
LEN(REPLACE(model, '1','11')) - LEN(model))+
2*(LEN(REPLACE(model, '2','22')) - LEN(model)) +
3*(LEN(REPLACE(model, '3','33')) - LEN(model)) +
4*(LEN(REPLACE(model, '4','44')) - LEN(model)) +
5*(LEN(REPLACE(model, '5','55')) - LEN(model)) +
6*(LEN(REPLACE(model, '6','66')) - LEN(model)) +
7*(LEN(REPLACE(model, '7','77')) - LEN(model)) +
8*(LEN(REPLACE(model, '8','88')) - LEN(model)) +
9*(LEN(REPLACE(model, '9','99')) - LEN(model)) as Suma
from produse where (model%2)=0

15

exec
M18

19. S se calculeze suma cifrelor numerelor fiecrui mode din tabelul produse. S se afi eze
modelul i suma. De exemplu, dac modelul este 1121, atunci suma cifrelor din model este 5.
use DBlab8
go
if OBJECT_ID('M19', 'P') is not null drop procedure M19 go
create procedure M19
as
select model,
(
LEN(REPLACE(model, '1','11')) - LEN(model))+
2*(LEN(REPLACE(model, '2','22')) - LEN(model)) +
3*(LEN(REPLACE(model, '3','33')) - LEN(model)) +
4*(LEN(REPLACE(model, '4','44')) - LEN(model)) +
5*(LEN(REPLACE(model, '5','55')) - LEN(model)) +
6*(LEN(REPLACE(model, '6','66')) - LEN(model)) +
7*(LEN(REPLACE(model, '7','77')) - LEN(model)) +
8*(LEN(REPLACE(model, '8','88')) - LEN(model)) +
9*(LEN(REPLACE(model, '9','99')) - LEN(model)) as Suma from
produse
exec
M19

20. Din tabelul produse s se obin un tabel cu coloane Producator, PC, Imprimante; Laptop_uri.
Pentru fiecare productor, n acest tabel, s se indice dac acesta produce sau nu (da sau nu) un
anumit tip de produse. De exemplu, un rnd din tabelul construit poate arata: A, nu, da, nu.
use DBlab8
go
if OBJECT_ID('M20', 'P') is not null drop procedure M20 go
create procedure M20
as
select producator,
REPLACE(case when [pc]>0then 'Da'else 'Nu' end+'('+
convert(varchar,[pc])+')','Nu(0)','Nu') AS pc,
REPLACE(case when [laptop_uri]>0 then 'Da'else 'Nu' end+'('+
convert(varchar,[laptop_uri]) +')','Nu(0)','Nu') as laptop_uri,
REPLACE(case when [imprimante]>0then 'Da'else 'Nu' end+'('+
convert(varchar,[imprimante]) +')','Nu(0)','Nu') as imprimante from
(
select A.producator,b.model,a.tip from produse a JOIN
pc_uri b ON a.model=b.model group by
A.producator,b.model,a.tip
union all
select A.producator,b.model,a.tip from produse a
JOIN laptop_uri b ON a.model=b.model group by
A.producator,b.model,a.tip
union all
select A.producator,b.model,a.tip from produse a JOIN
imprimante b ON a.model=b.model group by
A.producator,b.model,a.tip) main pivot (count(model)
for
[tip] in ([pc],[laptop_uri],[imprimante])) pvt

16

exec
M20

Sarcina 2
use DBlab8
go
if OBJECT_ID('S2', 'P') is not null drop procedure S2 go
create procedure S2
@Producator char(1),
@Tip varchar(10)
as
if @Tip='all'
delete produseS2 where Producator=@Producator
else
delete produseS2 where Producator=@Producator and Tip=@Tip
exec S2 A, 'all' exec S2
D, 'imprimante'

Sarcina 3
use DBlab8
go
if OBJECT_ID('S3', 'P') is not null drop procedure S3
go
create procedure S3
@Id int,
@Model varchar(4),
@Color char(2),
@Tip varchar(6),
@Pret float
as
declare @errorMessage varchar(71)='Datele sunt incomplete sau gresite, verificati corectitudinea
acestora.'
if( (@Id is null or @Id=' ')
or (@Model is
null or @Model=' ')
or (@Color is null or
@Color=' ')
or (@Tip is null or @Tip=' ')
or (@Pret is null or @Pret=' ')) print @errorMessage else
insert imprimante (id, model, color, tip, pret)
values (@Id, @Model, @Color, @Tip, @Pret)
exec S3 888,'9999', 'da', 'Laser',1000
select * from imprimante delete
imprimante where id=8888

Sarcina 4
use DBlab8
go
if OBJECT_ID('S4', 'P') is not null drop procedure S4 go
create procedure S4
@NrProduse int
as
select producator as Producator, tip as Produs, count(model) as Nr_De_Modele from produse
group by producator ,tip having Producator
in (select Producator from produse
group by producator having count(distinct tip)=@NrProduse)
print 'Acest producator are la dispozitie '+RTRIM(cast(@NrProduse as varchar(10)))+' tipuri de
produse.' exec S4 3

17

Sarcina 5
use DBlab8
go
if OBJECT_ID('S5', 'P') is not null drop procedure S5
go
create procedure S5
@Model varchar(5),
@Unitati int,
@Ru float
as
declare @Pret float
declare @PC float=( select Pret from pc_uriS5
where @Model=Model+Identificator)
declare @Laptop float=( select Pret from laptop_uriS5
where @Model=Model+Identificator)
declare @Imprimante float=( select Pret from imprimanteS5
where @Model=Model+Identificator)
declare @Rt float
declare @Rp float
declare @Suma
float
if(@Model in (select Model+Identificator from pc_uriS5
where @Model=Model+pc_uriS5.Identificator ))
BEGIN
set @Rt=10
set
@Pret=@PC et2:
if(@Pret<=400)
begin
set @Rp =3 et1:
set @Suma =@Pret * @Unitati(@Pret*@Unitati*(@Ru/100+@Rt/100+@Rp/100))
print 'Suma totala pentru aceaste cumparaturi este in valoare de '+RTRIM(cast(@Suma as
varchar(10)))
goto final
end
else if(@Pret<=800)
begin
set @Rp =6
goto et1
end
else
set @Rp=10
goto et1
END
ELSE
if(@Model in (select Model+Identificator from laptop_uriS5
@Model=Model+Identificator ))
begin
set
@Rt=15
set @Pret=@Laptop
goto et2
end
else

18

where

if(@Model in (select Model+Identificator from imprimanteS5


@Model=Model+Identificator ))

where

set @Rt=5
set @Pret=@Imprimante
goto et2
final:
exec S5 '1433B',10, 5
select * from pc_uriS5
select * from laptop_uriS5
select * from imprimanteS5

alter table pc_uriS5 alter column


model varchar(5) alter table
laptop_uriS5 alter column model
varchar(5) alter table imprimanteS5
alter column model varchar(5)
alter table pc_uriS5
add Identificator as char(Id+64) alter
table laptop_uriS5
add Identificator as char(Id+64) alter
table imprimanteS5
add Identificator as char(Id+64)

Lista de reduceri promoionale


n funcie de numrul de uniti de marf
0-10
10-20
20-30
30-50
n dependen de tipul de marf procurat
Imprimante
Calculatoare
Laptop-uri
n dependen de preul produsului fr reducere
<=400
500< and <=800
>800

Procentul de reducere, %
5
7
10
20
Procentul de reducere, %
5
10
15
Procentul de reducere, %
3
6
10

Sarcina 6
Interogri simple
1. S se gseasc modelul, viteza procesorului i capacitatea discului dur pentru toate pc-urile care
cost mai puin de 500 $. Schema rezultatului afiat va fi Model, Viteza i HD.
USE DBlab8
GO
if OBJECT_ID ('FNS1','FN') IS NOT NULL DROP FUNCTION FNS1;
GO
Create function FNS1 (@Pret float)
RETURNS TABLE

19

AS
RETURN(
select Model, Viteza, HD from pc_uri where Pret< @Pret
)
Select * from FNS1(500)

2. S se gseasc productorii de imprimante. Afiai: Producator.


USE DBlab8
GO
if OBJECT_ID ('FNS2','FN') IS NOT NULL DROP FUNCTION FNS2;
GO
Create function FNS2 (@Tip varchar(10))
RETURNS TABLE
AS
RETURN(
select Producator from produse where Tip=@Tip group by Producator
)
Select * from FNS2('Imprimante')

3. S se gseasc modelul, capacitatea de memorie operativ i dimensiunea ecranului fiecrui


laptop cu preul mai mare de 1000 $.
USE DBlab8
GO
if OBJECT_ID ('FNS3','FN') IS NOT NULL DROP FUNCTION FNS3;
GO
Create function FNS3 (@Pret float)
RETURNS TABLE
AS
RETURN(
select Model, RAM, Ecran from laptop_uri where Pret>1000
)
Select * from FNS3(1000)

4. S se gseasc toate liniile tabelului imprimante pentru imprimantele color.


USE DBlab8
GO
if OBJECT_ID ('FNS4','FN') IS NOT NULL DROP FUNCTION FNS4; GO
Create function FNS4 (@Color char(2) )
RETURNS TABLE
AS
RETURN(
select * from imprimante where Color=@Color
)
Select * from FNS4('da')

5. S se gseasc modelul i viteza procesorului i capacitatea discului dur ale pc-urilor mai ieftine
de 600 $ i care posed cd-rom-urilor cu viteze 12x i 24x.
USE DBlab8
GO
if OBJECT_ID ('FNS5','FN') IS NOT NULL DROP FUNCTION FNS5;
GO

20

Create function FNS5 (@Pret float,


@CD1 varchar(3),
@CD2 varchar(3)
)
RETURNS TABLE
AS
RETURN(
select Model, Viteza, HD from pc_uri where
pret<@Pret and (cd=@CD1 or cd=@CD2)
)
Select * from FNS5(600, '12x', '24x')

6. S se afieze productorii i viteza pc-urilor ce posed HD-uri cu capacitatea cea mai mic.
USE DBlab8
GO
if OBJECT_ID ('FNS6','FN') IS NOT NULL DROP FUNCTION FNS6;
GO
Create function FNS6 (@minHD decimal(2,0))
RETURNS TABLE
AS
RETURN
(
select producator, viteza
from
produse inner join pc_uri
on produse.model=pc_uri.model and hd=@minHD
)
declare @min decimal(2,0)= (Select MIN(HD) from pc_uri) Select *
from FNS6(@min)

7. S se gseasc modelele i productorii produselor cu preul mai mare de 450 $.


USE DBlab8
GO
if OBJECT_ID ('FNS7','FN') IS NOT NULL DROP FUNCTION FNS7;
GO
Create function FNS7 (@Pret float)
RETURNS TABLE
AS
RETURN(
select distinct l.Producator, l.Model from produse as l
left join pc_uri as p on l.Model=p.Model left join
laptop_uri as k on l.Model=k.Model left join imprimante
as i on l.Model=i.Model where p.Pret>@Pret or
k.Pret>@Pret or i.Pret>@Pret
)
Select * from FNS7(450)

8. S se gseasc productorii care vnd laptopuri sau imprimante, dar nu vnd pc-uri.
USE DBlab8
GO
if OBJECT_ID ('FNS8','FN') IS NOT NULL DROP FUNCTION FNS8;
GO
Create function FNS8 (@Tip varchar(10))
RETURNS TABLE
AS
RETURN(
select distinct Producator

21

from produse
where Producator not in
(select Producator from produse where Tip=@Tip)
)
Select * from FNS8('PC')

9. S se gseasc productorii de pc-uri cu procesoare ce funcioneaz ca frecven a de tact nu mai


mic de 450 MHz. Afiai cmpul Productor.
USE DBlab8
GO
if OBJECT_ID ('FNS9','FN') IS NOT NULL DROP FUNCTION FNS9;
GO
Create function FNS9 (@Viteza decimal(3,0))
RETURNS TABLE
AS
RETURN(
select distinct Producator
from produse as l inner join pc_uri as p on
l.Model=p.Model and Viteza>@Viteza
)
Select * from FNS9(450 )

10. S se gseasc imprimantele cu cel mai nalt pre. S se afieze Model, Pre.
USE DBlab8
GO
if OBJECT_ID ('FNS10','FN') IS NOT NULL DROP FUNCTION FNS10;
GO
Create function FNS10 ()
RETURNS TABLE
AS
RETURN(
select Model, Pret from imprimante
where Pret=(Select MAX(Pret) from imprimante) )
Select * from FNS10()

11. S se gseasc viteza medie a pc-urilor.


USE DBlab8
GO
if OBJECT_ID ('FNS11','FN') IS NOT NULL DROP FUNCTION FNS11; GO
Create function FNS11 ()
RETURNS Float
Begin
declare @VitezaMedie float
select @VitezaMedie=avg(viteza) from pc_uri
return @VitezaMedie
end
Select dbo.FNS11() as VitezaMedie

12. S se gseasc viteza medie a laptopurilor, al cror pre depete 1000 $.


USE DBlab8
GO
if OBJECT_ID ('FNS12','FN') IS NOT NULL DROP FUNCTION FNS12;

22

GO
Create function FNS12 (@Pret float)
RETURNS float begin
declare @VitezaMedie float
select @VitezaMedie=AVG(Viteza) from laptop_uri
where Pret>@Pret
return @VitezaMedie end
Select dbo.FNS12(1000) as VitezaMedie

13. S se gseasc viteza medie a pc-urilor produse de compania A.


USE DBlab8
GO
if OBJECT_ID ('FNS13','FN') IS NOT NULL DROP FUNCTION FNS13;
GO
Create function FNS13 (@Producator char(1))
RETURNS float begin
declare @VitezaMedie float
select distinct @VitezaMedie=avg(Viteza)
from pc_uri as l
inner join produse as p
on l.Model= p.Model and
Producator= @Producator return @VitezaMedie end
Select dbo.FNS13('A') as VitezaMedie

14. S se gseasc modelele i productorii pc-urilor care au o vitez mai mare dect media ramului laptopurilor.
USE DBlab8
GO
if OBJECT_ID ('FNS14','FN') IS NOT NULL DROP FUNCTION FNS14; GO
Create function FNS14 ()
RETURNS TABLE
AS
RETURN(
select l.Model, l.Producator, p.Viteza
from produse as l inner join pc_uri as p
on l.Model=p.Model and Viteza>(Select MAX(Viteza) from laptop_uri) )
Select * from FNS14()

15. Afiarea tuturor productorilor pc-urilor ce au ram-ul mai mare dect preul dublu al celui mai
ieftin pc. S se afieze modelul i productorul pc-urilor.
USE DBlab8
GO
if OBJECT_ID ('FNS15','FN') IS NOT NULL DROP FUNCTION FNS15;
GO
Create function FNS15 ()
RETURNS TABLE
AS
RETURN(
Select distinct p.Producator, p.Model
from pc_uri as
l inner join produse as p
on l.Model=p.Model and RAM>(Select AVG(RAM) from laptop_uri) )
Select * from FNS15()

23

16. S se gseasc modelele laptopurilor cu preul mai mare dect preul dublu al celui mai ieftin
pc.
S se afieze modelul laptopului i preul acestuia.
USE DBlab8
GO
if OBJECT_ID ('FNS16','FN') IS NOT NULL DROP FUNCTION FNS16;
GO
Create function FNS16 ()
RETURNS TABLE
AS
RETURN(
select Model, Pret from laptop_uri
where Pret>(Select MIN(Pret) from pc_uri)*2
)
Select * from FNS16()

17. Pentru fiecare valoare a vitezei, s se gseasc preul mediu al pc-urilor. S se afieze Viteza i
Pret_Mediu.
USE DBlab8
GO
if OBJECT_ID ('FNS17','FN') IS NOT NULL DROP FUNCTION FNS17;
GO
Create function FNS17 ()
RETURNS TABLE
AS
RETURN(
select Viteza, AVG(Pret)as PretMediu from pc_uri
group by Viteza
)

Interogri multi-relaie
1. S se gseasc capacitile discurilor dure, care coincid cu 2 sau mai multe pc-uri. S se afi eze
HD.
USE DBlab8
GO
if OBJECT_ID ('FNM1','FN') IS NOT NULL DROP FUNCTION FNM1;
GO
Create function FNM1()
RETURNS TABLE
AS
RETURN(
select HD from pc_uri
group by HD having count(HD)>1
)
Select * from FNM1()

2. S se gseasc perechile de modele de pc-uri, care posed procesoare cu aceiai vitez i


memorie ram cu aceiai capacitate. Ca rezultat, fiecare pereche se afieaz o singur dat, adic
24

(i,j), dar nu (i,j). Ordinea de afiare: model cu numr mai mare, modelul cu numr mai mic,
viteza i Ram.
USE DBlab8
GO
if OBJECT_ID ('FNM2','FN') IS NOT NULL DROP FUNCTION FNM2;
GO
Create function FNM2 ()
RETURNS TABLE
AS
RETURN(
select distinct p.Model, l.Model as Modell, l.Viteza, l.RAM
from pc_uri as p, pc_uri as l
where p.Viteza=l.Viteza and p.RAM=l.RAM and p.Model>l.Model )
Select * from FNM2()

3. S se gseasc laptopurile, a cror vitez este mai mic dect la orice pc. S se afi eze: Tip,
Model, Viteza.
USE DBlab8
GO
if OBJECT_ID ('FNM3','FN') IS NOT NULL DROP FUNCTION FNM3;
GO
Create function FNM3 ()
RETURNS TABLE
AS
RETURN(
select p.Tip, p.Model, l.Viteza
from laptop_uri as l inner join produse as p on l.Model=p.Model and
l.Viteza<(select MIN(Viteza) from pc_uri) )
Select * from FNM3()

4. S se gseasc productorii celor mai scumpe laptopuri sau pc-uri. Afiai Producator, Model i
Pre.
USE DBlab8
GO
if OBJECT_ID ('FNM4','FN') IS NOT NULL DROP FUNCTION FNM4; GO
Create function FNM4 ()
RETURNS TABLE
AS
RETURN(
select produse.Producator, produse.Model, Pret from produse, pc_uri where
produse.Model=pc_uri.Model and Pret=(Select max(Pret) from pc_uri) union
select produse.Producator, produse.Model, Pret from produse, laptop_uri
produse.Model=laptop_uri.Model and Pret=(Select max(Pret) from laptop_uri) )

where

Select * from FNM4()

5. S se gseasc dimensiunea medie a ecranului laptopurilor produse de fiecare productor. S se


afieze Producator i dimensiunea medie a ecranului.
USE DBlab8
GO

25

if OBJECT_ID ('FNM5','FN') IS NOT NULL DROP FUNCTION FNM5;


GO
Create function FNM5 ()
RETURNS TABLE
AS
RETURN(
select produse.Producator, avg(laptop_uri.Ecran) as DimensiuneEcran from
produse , laptop_uri
where produse.Model=laptop_uri.Model group by produse.Producator )
Select * from FNM5()

6. S se gseasc productorii, care produc cel puin 3 pc-uri. Afiai Producator, numrul de
modele.
USE DBlab8
GO
if OBJECT_ID ('FNM6','FN') IS NOT NULL DROP FUNCTION FNM6;
GO
Create function FNM6 (@pcNumber int)
RETURNS TABLE
AS
RETURN(
select produse.Producator, count(*) as NumarPC
from pc_uri inner join produse
on pc_uri.Model=produse.Model
group by Producator having count(*)>=@pcNumber
)
Select * from FNM6(3)

7. S se gseasc preul maximal al pc-urilor propuse de fiecare productor. Afi a i Producator,


pre maxim.
USE DBlab8
GO
if OBJECT_ID ('FNM7','FN') IS NOT NULL DROP FUNCTION FNM7; GO
Create function FNM7 ()
RETURNS TABLE
AS
RETURN(
select produse.Producator, max(pc_uri.Pret) as PretMaxim
from produse, pc_uri
where produse.Model=pc_uri.Model group by Producator
)
Select * from FNM7()

8. Pentru fiecare valoare a vitezei pc-urilor care depete 600 Mh, s se determine preul mediu al
USE DBlab8
GO
if OBJECT_ID ('FNM8','FN') IS NOT NULL DROP FUNCTION FNM8;
GO
Create function FNM8 (@Viteza decimal(3,0))
RETURNS TABLE
AS
RETURN(
select Viteza, avg(Pret) as PretMediu
from pc_uri

26

group by Viteza having Viteza>@Viteza


)
Select * from FNM8(600)

9. Afiarea tuturor productorilor care produc pc-uri i laptopuri care dispun de o vitez nu mai
mic de 750 Mh. Afiai Producator.
USE DBlab8
GO
if OBJECT_ID ('FNM9','FN') IS NOT NULL DROP FUNCTION FNM9;
GO
Create function FNM9 (@Viteza decimal(3,0),
@Tip1 varchar(10),
@Tip2 varchar(10)
)
RETURNS TABLE
AS
RETURN(
select distinct produse.Producator from
produse, pc_uri, laptop_uri
where
produse.Model=pc_uri.Model
and
(Tip=@Tip1 or Tip=@Tip2)
and
pc_uri.Viteza>=@Viteza
or
produse.Model=laptop_uri.Model
and laptop_uri.Viteza>=@Viteza
)
Select * from FNM9(750,'PC', 'Laptop_uri')

10. S se gseasc suma total a preurilor celor mai ieftine imprimante de fiecare tip.
USE DBlab8
GO
if OBJECT_ID ('FNM10','FN') IS NOT NULL DROP FUNCTION FNM10;
GO
Create function FNM10 (@Tip1 varchar(10),
@Tip2 varchar(10),
@Tip3 varchar(10)
)
RETURNS float begin
declare @a int, @b int, @c int ,@d float
set @a=(select min(Pret) from imprimante where Tip=@Tip1)
set @b=(select min(Pret) from imprimante where Tip=@Tip2) set
@c=(select min(Pret) from imprimante where Tip=@Tip3)
select distinct @d= @a+@b+@c from
imprimante return @d end
Select dbo.FNM10('Jet', 'Matrix','Laser') as PretTotal

11. S se gseasc modelul produsului cu cel mai mare pre. Se afieaz Model.
USE DBlab8
GO
if OBJECT_ID ('FNM11','FN') IS NOT NULL DROP FUNCTION FNM11;
GO
Create function FNM11 ()
RETURNS INT
BEGIN
DECLARE @Rezultat FLOAT

27

DECLARE @a float = (select max(imprimante.Pret) from imprimante)


DECLARE @b float = (select max(laptop_uri.pret) from laptop_uri)
DECLARE @c float = (select imprimante.Model from imprimante where imprimante.Pret = (select
min(imprimante.Pret) from imprimante))
DECLARE @d FLOAT = (select pc_uri.Model from pc_uri where pc_uri.Pret = (select max(pc_uri.Pret)
from pc_uri))
DECLARE @e float = (select laptop_uri.Model from laptop_uri where laptop_uri.Pret = (select
max(laptop_uri.Pret) from laptop_uri))
DECLARE @f float = (select pc_uri.Model from pc_uri where pc_uri.Pret = (select max(pc_uri.Pret)
from pc_uri))
if(@a > @b)
goto et1 else
goto et2
et1: if((select max(imprimante.Pret) from imprimante)>(select max(pc_uri.Pret) from pc_uri))
SET @Rezultat = @c else
SET @Rezultat = @d return
@Rezultat
et2:
if((select max(laptop_uri.Pret) from laptop_uri)>(select max(pc_uri.Pret) from pc_uri))
SET @Rezultat = @e
else
SET @Rezultat = @f
RETURN @Rezultat
END
Select dbo.FNM11() as ModelPretMax

12. S se gseasc productorii de imprimante care produc i pc-uri cu cel mai mic volum ram. S
se afieze Producator.
USE DBlab8
GO
if OBJECT_ID ('FNM12','FN') IS NOT NULL DROP FUNCTION FNM12;
GO
Create function FNM12 (@Tip1 varchar(10),
@Tip2 varchar(10))
RETURNS TABLE
AS
RETURN(
select
distinct produse.Producator
from
produse,
pc_uri
where
produse.Model=pc_uri.Model
and
(Tip=@Tip1 or Tip=@Tip2)
and pc_uri.RAM=(select min(RAM) from pc_uri))
Select * from FNM12('PC','imprimante')

13. S se gseasc preul mediu al pc-urilor i laptopurilor produse de ctre A. S se afi eze
Pre_med_comun.
USE DBlab8
GO
if OBJECT_ID ('FNM','FN') IS NOT NULL DROP FUNCTION FNM;
GO
Create function FNM (@Producator char(1))
RETURNS float begin

28

declare @PretMediu float select


@PretMediu=avg(Tabel.Pret) from
(
select sum(pc_uri.Pret) as Pret
from produse,pc_uri
where produse.Model = pc_uri.Model and produse.Producator = @Producator union
select sum(laptop_uri.Pret) as Pret
from produse,laptop_uri
where produse.Model = laptop_uri.Model and produse.Producator = @Producator ) as Tabel
return @PretMediu end
Select dbo.FNM('A') as PretMediu

14. S se gseasc preul mediu al pc-urilor pentru fiecare din productori care produc i
imprimante.
Afiai Producator, Pret_med_prod.
USE DBlab8
GO
if OBJECT_ID ('FNM14','FN') IS NOT NULL DROP FUNCTION FNM14;
GO
Create function FNM14 ( @Tip1 varchar(10),
@Tip2 varchar(10)
)
RETURNS TABLE
AS
RETURN(
Select Producator, avg(pc_uri.Pret) as PretMediu
from produse,pc_uri
where produse.Model=pc_uri.Model and Producator in
(select distinct Producator from
produse as p
where Tip=@Tip1 and exists (select Producator from produse
where Tip=@Tip2 and Producator=p.Producator)) group by Producator )
Select * from FNM14('PC','imprimante')

15. S se gseasc capacitatea medie a HD pc-urilor ale acelor productori care produc i
imprimante. Afiai capacitatea medie HD.
USE DBlab8
GO
if OBJECT_ID ('FNM15','FN') IS NOT NULL DROP FUNCTION FNM15;
GO
Create function FNM15 (@Tip1 varchar(10),
@Tip2 varchar(10)
)
RETURNS TABLE
AS
RETURN(
Select Producator, avg(pc_uri.HD) as HDMediu
from produse,pc_uri
where produse.Model=pc_uri.Model and Producator in
(select distinct Producator from
produse as p
where Tip= @Tip1 and exists (select Producator from produse
where Tip=@Tip2 and Producator=p.Producator)) group by Producator )

29

Select * from FNM15('PC','imprimante')

16. S se gseasc productorii care produc numai un singur tip de produs. S se afi eze
productorul, tipul produsului, modelul i preul modelului respectiv.
USE DBlab8
GO
if OBJECT_ID ('FNM16','FN') IS NOT NULL DROP FUNCTION FNM16;
GO
Create function FNM16 ()
RETURNS TABLE
AS
RETURN(
Select p.Producator, produse.Model, m.Pret
from (Select Producator
from produse
group by Producator having count(Producator)=1 ) as p inner
join produse on p.Producator=produse.Producator
inner join
(select pc_uri.Model, Pret from pc_uri
union
select laptop_uri.Model, Pret from laptop_uri
union
select imprimante.Model, Pret from imprimante) as m on
produse.Model=m.Model
)
Select * from FNM16()

17. S se analizeze preurile laptop-urilor i n funcie de valoare s fie grupate n 3 clase. Dac este
mai ieftin de 750 $- clasa econom., dac preul nu depete 1100 $- clasa business, dac pre ul
este mai mare de 1100 $- clasa premium. S se afieze tipul produsului, Mode, Pret i clasa
produsului.
USE DBlab8
GO
if OBJECT_ID ('FNM17','FN') IS NOT NULL DROP FUNCTION FNM17;
GO
Create function FNM17 (@ClasaProdus1 varchar(10),
@ClasaProdus2 varchar(10),
@ClasaProdus3 varchar(10))
RETURNS TABLE
AS
RETURN(
select produse.Tip, laptop_uri.Model, laptop_uri.Pret,ProductClass.ClasaProdus from
laptop_uri
inner join BazaDeDateLab3.dbo.ProductClass on (laptop_uri.Pret<750 and
ClasaProdus=@ClasaProdus1)
or ((laptop_uri.Pret>750 and laptop_uri.Pret<1100) and ClasaProdus=@ClasaProdus2)
or (laptop_uri.Pret>1100 and ClasaProdus=@ClasaProdus3)
inner join
produse on produse.Model=laptop_uri.Model
)
Select * from FNM17('econom', 'business', 'premium')

30

18. n tabelul produse s se gseasc modelele la care prima cifr din model este o cifr impar, iar
ultima par i prima cifr este mai mic dect ultima. S se afi eze modelul, tipul produsului,
produsul primei i ultimei cifre din numrul modelului.
USE DBlab8
GO
if OBJECT_ID ('FNM18','FN') IS NOT NULL DROP FUNCTION FNM18;
GO
Create function FNM18 ()
RETURNS TABLE
AS RETURN(
select model,
(
LEN(REPLACE(model, '1','11')) - LEN(model))+
2*(LEN(REPLACE(model, '2','22')) - LEN(model)) +
3*(LEN(REPLACE(model, '3','33')) - LEN(model)) +
4*(LEN(REPLACE(model, '4','44')) - LEN(model)) +
5*(LEN(REPLACE(model, '5','55')) - LEN(model)) +
6*(LEN(REPLACE(model, '6','66')) - LEN(model)) +
7*(LEN(REPLACE(model, '7','77')) - LEN(model)) +
8*(LEN(REPLACE(model, '8','88')) - LEN(model)) +
9*(LEN(REPLACE(model, '9','99')) - LEN(model)) as Suma
from produse where (model%2)=0
)
Select * from FNM18()

19. S se calculeze suma cifrelor numerelor fiecrui mode din tabelul produse. S se afi eze
modelul i suma. De exemplu, dac modelul este 1121, atunci suma cifrelor din model este 5.
USE DBlab8
GO
if OBJECT_ID ('FNM19','FN') IS NOT NULL DROP FUNCTION FNM19;
GO
Create function FNM19 ()
RETURNS TABLE
AS RETURN(
select model,
(
LEN(REPLACE(model, '1','11')) - LEN(model))+
2*(LEN(REPLACE(model, '2','22')) - LEN(model)) +
3*(LEN(REPLACE(model, '3','33')) - LEN(model)) +
4*(LEN(REPLACE(model, '4','44')) - LEN(model)) +
5*(LEN(REPLACE(model, '5','55')) - LEN(model)) +
6*(LEN(REPLACE(model, '6','66')) - LEN(model)) +
7*(LEN(REPLACE(model, '7','77')) - LEN(model)) +
8*(LEN(REPLACE(model, '8','88')) - LEN(model)) +
9*(LEN(REPLACE(model, '9','99')) - LEN(model)) as Suma from
produse
)
Select * from FNM19()

20. Din tabelul produse s se obin un tabel cu coloane Producator, PC, Imprimante; Laptop_uri.
31

Pentru fiecare productor, n acest tabel, s se indice dac acesta produce sau nu (da sau nu) un
anumit tip de produse. De exemplu, un rnd din tabelul construit poate arata: A, nu, da, nu.
USE DBlab8
GO
if OBJECT_ID ('FNM20','FN') IS NOT NULL DROP FUNCTION FNM20;
GO
Create function FNM20 ()
RETURNS TABLE
AS
RETURN(
select producator,
REPLACE(case when [pc]>0then 'Da'else 'Nu' end+'('+
convert(varchar,[pc])+')','Nu(0)','Nu') AS pc,
REPLACE(case when [laptop_uri]>0 then 'Da'else 'Nu' end+'('+
convert(varchar,[laptop_uri]) +')','Nu(0)','Nu') as laptop_uri,
REPLACE(case when [imprimante]>0then 'Da'else 'Nu' end+'('+
convert(varchar,[imprimante]) +')','Nu(0)','Nu') as imprimante from
(
select A.producator,b.model,a.tip from produse a JOIN
pc_uri b ON a.model=b.model group by
A.producator,b.model,a.tip
union all select A.producator,b.model,a.tip from
produse a
JOIN laptop_uri b ON a.model=b.model group by
A.producator,b.model,a.tip
union all select A.producator,b.model,a.tip from
produse a JOIN imprimante b ON a.model=b.model
group by A.producator,b.model,a.tip) main pivot
(count(model) for
[tip] in ([pc],[laptop_uri],[imprimante])) pvt
)
Select * from FNM20()

Sarcina 7
create function Sarcina7 ()
returns table as return(
select *, coalesce(a.pret(select b.pret from pc_uri as b where b.id+1=a.id),pret) as diferenta from pc_uri
as a
) select * from Sarcina7()

Sarcina 8
USE DBlab8 GO
if OBJECT_ID ('FNSS8','FN') IS NOT NULL DROP FUNCTION FNSS8;
GO
Create function FNSS8 (@column as varchar(10))
RETURNS TABLE
AS
RETURN(
Select model,case @column
when 'viteza' then cast(viteza as varchar(10))+' GHz'
when 'ram' then cast(ram as varchar(10))+' GB' when 'hd'
then cast(hd as varchar(10))+' GB' when 'pret' then
cast(pret as varchar(10))+' $' when 'ecran' then
cast(ecran as varchar(10))+'"' end result from laptop_uri

32

)
Select
Select
Select
Select

* from FNSS8('viteza')
* from FNSS8('ram')
* from FNSS8('hd')
* from FNSS8('pret')

Select * from FNSS8('ecran')

Sarcina 9
USE DBlab8 GO
if OBJECT_ID ('FNSS9','FN') IS NOT NULL DROP FUNCTION FNSS9;
GO
Create function FNSS9(@valoare_pret float, @curs_BNM float, @valuta char(3))
RETURNS
MONEY as
begin return(
case @valuta
when 'EUR' then @valoare_pret*@curs_BNM
when 'MDL' then @valoare_pret*@curs_BNM
end ) end select
dbo.FNSS9(400,20.5758,'MDL') as result select
dbo.FNSS9(400, 0.9243,'EUR') as result

Sarcina 10
create table pc_uriS10
(idPC int not null,
modelPC
varchar(4) not null,
vitezaPC
decimal(3,0) not null,
ramPC
decimal(3,0) not null,
hdPC
decimal(2,0) not null,
cdPC
varchar(3) not null,
pretPC float not
null,
constraint idd1S10 primary key(idPC));
create table laptop_uriS10
(idLaptop int not null,
modelLaptop varchar(4) not null,
vitezaLaptop decimal(3,0) not null,
ramLaptop decimal(3,0) not null,
hdLaptop decimal(2,0) not null,
pretLaptop float not null,
ecranLaptop int,
constraint idd2S10 primary key(idLaptop));
create table imprimanteS10
(idImprimante int not null,
modelImprimante varchar(4) not null,
colorImprimante char(2) not null,
tipImprimante
varchar(6) not null,
pretImprimante float not null,
constraint idd3S10 primary key(idImprimante ));
insert into pc_uriS10
select * from pc_uri insert
into laptop_uriS10 select *
from laptop_uri insert into
imprimanteS10 select *
from imprimante
select * from pc_uriS10
select * from laptop_uriS10
select * from imprimanteS10

33

create function task10(@tip_produs varchar(10), @pret_min float, @pret_max float)


returns table as return(
select tab.*, case @tip_produs when
'PC'then tab.pretPC
when 'Laptop-uri' then tab.pretLaptop
when 'Imprimante' then tab.pretImprimante
else 0 end pret
from (select * from produse left join pc_uriS10 on modelPC=produse.model and (pretPC>@pret_min
and pretPC<@pret_max)
left join laptop_uriS10 on modelLaptop=produse.model and
(pretLaptop>@pret_min and pretLaptop<@pret_max)
left join imprimanteS10 on
modelImprimante=produse.model and (pretImprimante>@pret_min and pretImprimante<@pret_max))
as tab
)
drop function task10
select distinct task10.idPC,
task10.producator,
task10.tip,
task10.model,
task10.vitezaPC,
task10.ramPC,
task10.hdPC,
task10.cdPC,
task10.pretPC
from pc_uriS10 inner join task10('PC',350,800)
on task10.modelPC=pc_uriS10.modelPC order by producator
select distinct task10.idLaptop,
task10.producator,
task10.tip,
task10.model,
task10.vitezaLaptop,
task10.ramLaptop,
task10.hdLaptop,
task10.pretLaptop,
task10.ecranLaptop
from laptop_uriS10 inner join task10('Laptop-uri',700,2000)
on task10.modelLaptop=laptop_uriS10.modelLaptop order by producator
select distinct task10.idImprimante,
task10.producator,
task10.tip,
task10.model,
task10.colorImprimante,
task10.tipImprimante,
task10.pretImprimante
from imprimanteS10 inner join task10('Imprimante',100,350)
on task10.modelImprimante=imprimanteS10.modelImprimante order by
producator

Sarcina 11
create function task11(@NumeProducator char(1), @flag bit)
returns table as return(
select * ,case @flag
when 1 then tab.pretMin
when 0 then
tab.pretMax end Pret

34

from (select * from maxim(@NumeProducator) inner join minim(@NumeProducator) on


maxim.producator=minim.Producatorul) as tab)
select producator,TIPmax,pretMax from task11('A',0) select
producator,TIPmin,pretMin from task11('A',1) drop function
task11
create function minim(@NumeProducator char(1))
returns table as return(
select top 1 tab.producator as Producatorul, tab.tip as TIPmin , min(tab.pretMin) as pretMin from
(select producator, produse.tip, min(pret) as pretMin from pc_uri inner join produse on
produse.model=pc_uri.model group by producator,produse.tip union all
select producator,produse.tip, min(pret) as pretMin from laptop_uri inner join produse on
produse.model=laptop_uri.model group by producator,produse.tip union all
select producator, produse.tip,min(pret) as pretMin from imprimante inner join produse on
produse.model=imprimante.model group by producator,produse.tip) as tab where
producator=@NumeProducator
group by tab.producator,tip order by pretMin
) select * from minim('A')
drop function minim
create function maxim(@NumeProducator
char(1)) returns table as return(
select top 1 tab.producator, tab.tip as TIPmax, max(tab.pretMax) as pretMax from (select
producator, produse.tip, max(pret) as pretMax from pc_uri inner join produse on
produse.model=pc_uri.model group by producator,produse.tip union all
select producator,produse.tip, max(pret) as pretMax from laptop_uri inner join produse on
produse.model=laptop_uri.model group by producator,produse.tip union all
select producator, produse.tip,max(pret) as pretMax from imprimante inner join produse on
produse.model=imprimante.model group by producator,produse.tip) as tab where
producator=@NumeProducator
group by tab.producator,tip order by pretMax desc
) select * from
maxim('A')
drop function maxim

35

Concluzii
Acest laborator cuprinde aspecte practice despre structura, crearea, modificarea i executarea
procedurilor stocate i funciilor definite de utilizator. Avnd la baz instruciuni SQL, procedura
stocat sau funcia realizeaz o anumit operaie, reprezentnd n final, un program SQL. Unul dintre
avantajele utilizrii procedurilor stocate, ct i a funciilor este stocarea acestora n baza de date SQL i
apelarea ori de cte ori este nevoie. Ele reduc costurile la compilarea codului Transact-SQL, timpul de
execuie i menine n cache planurile pentru reutilizarea n curs de executare repetat. Elementele de
control fiind executate ca un singur lot de coduri, reduc considerabil traficul de re ea ntre server i
client, datorit trimiterii doar al apelului pentru rularea procedurii sau funciei.

36