Vous êtes sur la page 1sur 9

LAKHDOURA YOUNESS

LAZAR ADNANE

TP 3

4-
Création de base de donne
create database DB_ligne_commande
a- Création des tables :
create table produit (
num_prod int primary key identity(1,1),
libelle varchar (50),
pu_prod real ,
qte_stock int ,
s_min int ,
s_max int

);

create table commande (


num_cmd int primary key identity(1,1),
date_cmd date ,
);

create table ligne_commande(


num_prod int foreign key references produit(num_prod),
num_cmd int foreign key references commande(num_cmd),
qte_cmdee int ,
primary key (num_prod,num_cmd)
)
b- remplissage des tables :
insert
into produit
values
('produit7','10000','8','5','50'),('produit8','100000','8','5','50'),('produit9','1000','8','5','
50'),('produit10','60','8','5','50')

insert
into commande
values
('2017-01-02'),('2017-03-10'),('2017-12-01'),('2017-12-12'),('2017-01-03'),('2017-03-
30')

insert into ligne_commande


values
('1','5','20'),('1','4','55'),('8','5','26'),('6','8','22'),('7','9','20'),('2','6','1')

5- Interrogation de cette base de données :


a-
Select num_prod,libelle, pu_prod, 'Etat' =
Case
When
qte_stock=0
then
'Non Disponible'
When
qte_stock>s_min
then 'Disponible'
Else 'à Commander'
End
From produit ;

b-
Declare @Montant decimal
Set @Montant=(
Select Sum(pu_prod*qte_stock)
from commande C, produit P, lignecommande LC
where C.num_cmd=LC.num_cmd and LC.num_prod=P.num_prod and
C.num_cmd=5)
If @Montant is null Begin
Print 'Cette Commande n''existe pas ' Return
End
if @Montant <=10000 Print 'Commande Normale'
Else
Print 'Commande Spéciale'

c-

Declare @qnt decimal


Set @qnt=(select qte_cmd
from lignecommande
where num_cmd=5 and num_prod=8)
Delete from lignecommande
where num_cmd=5 and num_prod=8
Update produit set qte_stock=qte_stock+@qnt
where num_prod=8
if not exists (select num_cmd
from lignecommande
where num_cmd=5)
Delete from commande
where num_cmd=5

6- Procédure stockées :
Exercice 1 :
a/ la liste des produits et leur libelle
create procedure affichage
as
select num_prod , libelle from produit

exec affichage

b/ nombre de produits par commande


create procedure affichage1
as
select num_cmd , count(num_prod) as nombre_produit_par_commande
from
ligne_commande
group by num_cmd
exec affichage1

c/ la liste des commandes effectuées entre deux dates


create procedure affichage2 (@date1 date,@date2 date)
as
select * from commande
where
date_cmd between @date1 and @date2

exec affichage2 '2017-06-01','2017-12-31'

exec affichage2

d/ nombre de commande ...


create procedure affichage3
as
declare @a int
set @a= (select count(num_cmd) as nombre_de_commande
from BD_LIGNE_COMMANDE.dbo.commande)
print @a

exec affichage

e/ nombre de produits d'une commande

alter procedure affichage4 (@a int)


as
declare @b int
set @b=(select count(num_prod) as "nombre de produit par commande"
from ligne_commande
where num_cmd=@a)
print @b
exec affichage4 "5"

Exercice 2 :
Créer une base de nom Exe2 :
create database Exercice2
- les tables mouvement ,produitfini et fournisseur :
créer la table produitfini
create table produitfini(
cod_prod_fini int primary key,
nom varchar(50),
qtestock int,
)

Créer table fournisseur


create table fournisseur(
num_fr int primary key,
nom_fr varchar(50),
adr_fr varchar(50),
nb_prod_fournis int,
)
a/ crée les tables produitbrut et composition :
create procedure pro1 as
create table produitbrut(
code_prod_brut int primary key,
nom varchar(50),
prix decimal,
num_fr int foreign key references fournisseur(num_fr)
)

create table composition(


qte_util decimal,
code_prod_fini int,
code_prod_brut int,
primary key(code_prod_fini,code_prod_brut),
foreign key(code_prod_fini) references
produitfini (code_prod_fini),
foreign key(code_prod_brut) references
produitbrut(code_prod_brut) )

exec pro1

b/ afficher le nomber de produits bruts par produit fini :


create procedure pro2 as
select code_prod_fini,count(*) as 'nombre des produit brut
par produit fini'
from composition
group by code_prod_fini

exec pro2

c/ le prix d’achat elevé :


create procedure pro3 @prix decimal output as
begin
set @prix=(select max(prix) from produitbrut)
end

declare @maxprix decimal


exec pro3 @maxprix output
print @maxprix

d/ pour chaque produit fini :


create procedure pro4 as
begin
declare @num int, @qte_stock decimal
declare @ qte_ total_entr decimal
declare @total_qte_sort decimal
declare chaque_prod cursor for select
code_prod_fini,qtestock from produitfini
open produits --debut du curseur
fetch next from chaque_prod into @num,@qte_stock
while @@fetch_status=0
begin
print 'La quntité produit'
+convert(varchar,@num)+' est: '
+convert(varchar,@qte_stock)
print 'la liste des mouvement concernant ce produit'
select * from mouvement
where code_prod_fini=@num
set @qte_total_entr =(select qte from mouvement
where code_prod_fini=@num AND type_mvt='entrer')
set @qte_total_sort =(select qte from mouvement
where code_prod_fini=@num AND type_mvt='sortir')
print ' la quantité total en entrée est:'
+convert(varchar,@qte_total_entr)
print ' la quantité total en sortie est: '
+convert(varchar,@qte_total_sort)
if @qte_total_entr - @qte_total_sort !=@qte_stock
print 'stock ok'
else print 'probléme de stock'
fetch next from chaque_prod into @num,@qte_stock
end
close produits –fin du curseur
deallocate chaque_prod
end

exec pro4

7- les fonctions :
Exercice :

a- fonction qui retourne le nombre des produits :


create function nbre_prod_existe() returns int
as begin
declare @nb int
set @nb=(select count(*) from produit)
return @nb
end
go
print dbo.nbre_prod_existe()

c- fonction return le nombre total des commandes :

create function nbre_commande_total() returns int


as begin
declare @nb int
set @nb=(select count(*) from commande)
return @nb
end
go
print dbo.nbre_commande_total()

e-fonction retourne le nombre de produit par commande :

create function nbre_prod_par_cmd(@num int) returns int


as begin
declare @nb int
set @nb=(select count(*) from lignecommande where
num_cmd=@num)
return @nb
end
go

print dbo. nbre_prod_par_cmd(5)

f- fonction retourne le nombre de produit par commande en retournant une table :


create function nbre_pro_par_cmd(@num int)
returns table
as
return select num_prod=num_prod,qte=qte_cmdee from
lignecommande where num_cmd=@num
go

select * from nbre_pro_par_cmd(5)

g- calculer le prix totale :


alter function prix_total(@num int)
returns decimal
as begin
declare @prix decimal
set @prix=(select sum(pu_prod*qte_cmdee) from lignecommande
l,produit p
where l.num_prod=p.num_prod AND l.num_cmd=@num)
return @prix
end
go
print dbo.prixtotal(5)

h- résultat sous la forme définie :

create function result(@num int) returns varchar(50)


as
begin
declare @prix decimal
set @prix=dbo.prixtotal(@num)
return 'le prix total de commande '+convert(varchar,@num)+' est:
'+convert(varchar,@prix)
end
go
print dbo.result(5)
j/ la commande du prix élevé :
create function cmd_prix_eleve()
returns @cmdprix table (num_cmd int,prix_cmd decimal)
as
begin
declare @max decimal
set @max=(select max(pu_prod) from produit)
insert into @cmdprix(num_cmd,prix_cmd)
select cmd_num=num_cmd,prix_prod=p.pu_prod
from lignecommande l, produit p
where p.num_prod=l.num_prod AND p.pu_prod=@max
return
end
go

select * from dbo.cmd_prix_eleve()


k/ afficher l’état d’un commande :
create function etat_de_cmd(@num int,@x int,@y int) returns
varchar(10)
as
begin
declare @nb int
set @nb=dbo.nbprod_par_cmd(@num)
declare @etat varchar(10)
set @etat=
case
when @nb<@x then 'petite'
when @nb<@y and @nb>@x then 'moyenne'
else 'grande'
end
return @etat
end
go
print etat_de_cmd(5,1,15)

l/ table de numéro de commande et son état :


create function etat_cmde(@x int,@y int)
returns @etat table (num_cmd int,eta varchar(10))
as
begin
declare @n int,@e varchar(10)
declare ligne cursor for select num_cmd from ligne_commande
open ligne
fetch next from ligne into @n
while @@fetch_status=0
begin
set @e=dbo.etat_de_cmd(@n,@x,@y)
insert into @etat(num_cmd,eta) select @n,@e
fetch next from ligne into @n
end
close ligne
deallocate ligne
return
end

go
select * from etat_cmde(1,5)

Vous aimerez peut-être aussi