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 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
| -- uniquement pour le test
drop database if exists hotels;
create database hotels default character set utf8 collate utf8_general_ci;
-- on sélectionne la bd quoi qu'il arrive pour bien travailler avec elle par défaut
use hotels; -- à toi d'adapter
-- on définit les tables et on les remplit uniquement pour le test
create table hotel(
id int(4) not null auto_increment,
nom varchar(255) not null,
constraint pk_hotel primary key(id)
)engine=myisam auto_increment=1;
create table personne(
id int(4) not null auto_increment,
nom varchar(255) not null,
constraint pk_personne primary key(id)
)engine=myisam auto_increment=1;
-- la table de liaison, je ne mets pas de contrainte dessus car je la crée avec le moteur myisam qui ne s'en occupe pas
create table personne_a_dormi(
id int(4) not null auto_increment,
idPersonne int(4) not null,
idHotel int(4) not null,
constraint pk_personne_a_dormi primary key(id)
)engine=myisam auto_increment=1;
insert into hotel(nom)values("hotel1"),("hotel2"),("hotel3");
insert into personne(nom)values("personne1"),("personne2"),("personne3"),("personne4");
insert into personne_a_dormi(idPersonne,idHotel)values(1,1),(1,2),(2,2),(3,1),(3,3),(4,1),(4,2),(4,3);
-- on définit la procédure stockée
drop procedure if exists recapitulatif;
delimiter |
create procedure recapitulatif()
begin
declare ligne text default "personne";
declare tmp varchar(255);
declare done,test bool;
declare cpers,chotel int(4);
declare colonnes cursor for select nom from hotel;
declare lignes cursor for select id,nom from personne;
declare lithotel cursor for select id from hotel;
declare CONTINUE HANDLER for SQLSTATE '02000' set done = 1;
-- on crée l'entête
open colonnes;
set done=false;
repeat
fetch colonnes into tmp;
if not done then
set ligne=concat(ligne,",",tmp);
end if;
until done end repeat;
close colonnes;
select ligne as entete;
set done=false;
open lignes;
repeat
fetch lignes into cpers,tmp;
if not done then
set test=done;
set ligne=tmp;
open lithotel;
repeat
fetch lithotel into chotel;
if not done then
select if(count(id)>0,"Oui","Non") into tmp from personne_a_dormi where idPersonne=cpers and idHotel=chotel;
set ligne=concat(ligne,",",tmp);
end if;
until done end repeat;
close lithotel;
select ligne;
set done=test;
end if;
until done end repeat;
close lignes;
end|
delimiter ;
-- fin de la procédure stockée
-- on teste en appelant la procédure
call recapitulatif(); |
Partager