1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
| CREATE PROCEDURE SupprEd(@titre varchar(64), @complément varchar(128), @V_SORTIE INT =1 OUTPUT)
as
declare @t_original varchar(64), @annee varchar(4), @stock int, @cpted int, @bool int
declare CurEd cursor for(
select TITRE_ORIGINAL, DATE_DE_SORTIE
from EDITION
where TITRE = @titre and COMPLEMENT_DE_TITRE = @complément)
open CurEd
fetch next from CurEd
into @t_original, @annee
-- On teste si l'édition existe, si ce n'est pas le cas, on affiche un message d'erreur
if(@@FETCH_STATUS != 0) print 'Aucune édition de ce type.'
-- Tant que le curseur CURED pointe sur un élément, on effectur les opérations suivantes
begin tran
while @@FETCH_STATUS = 0
begin
-- S'il une réservation a été faite sur l'édition, on affiche un message d'erreur
if(exists(select * from RESERVATION R where R.TITRE = @titre and R.COMPLEMENT_DE_TITRE = @complément))
begin
print 'Une réservation est prévue sur cette édition...'
set @V_SORTIE = 0
end
-- Sinon on supprime l'édition et les stocks correspondants
else
begin
declare CurStockAux cursor for(
select #STOCK
from STOCK
where TITRE = @titre and COMPLEMENT_DE_TITRE = @complément)
open CurStockAux
fetch next from CurStockAux
into @stock
set @cpted = 0
while @@FETCH_STATUS = 0
begin
-- La procédure SupprStock renvoie 1 si elle s'est déroulée correctement, 0 sinon
EXECUTE SupprStock @stock, @V_SORTIE = @bool OUTPUT
-- Si la suppression d'un élément du stock ne s'est pas effectuée correctement (location en cours),
-- on annule la transaction.
if(@bool = 0)
rollback tran
set @cpted = @cpted + 1
fetch next from CurStockAux
into @stock
end
close CurStockAux
deallocate CurStockAux
delete EDITION where TITRE = @titre and COMPLEMENT_DE_TITRE = @complément
end
print 'L''édition ' + @titre + ' : ' + @complément + ' et les stocks correspondants (' + CAST(@cpted as varchar) + ') ont été supprimés de la base.'
fetch next from CurEd
into @t_original, @annee
end
commit tran
close CurEd
deallocate CurEd; |
Partager