Précédent   Forum des professionnels en informatique > Bases de données > MySQL > SQL Procédural
SQL Procédural Forum d'entraide sur les triggers, les procédures stockées et les fonctions en MySQL
Partagez cette discussion sur d'autres réseaux sociaux : Viadeo Twitter Google Facebook Digg Delicious MySpace Yahoo
Réponse Proposer ce sujet en actualité
 
Outils de la discussion
Publicité
'
Vieux 16/06/2006, 10h57   #1
Invité de passage
 
Inscription : mai 2006
Messages : 21
Détails du profil
Informations forums :
Inscription : mai 2006
Messages : 21
Points : 4
Points : 4
Par défaut Executer une chaine concatener dans une procedure stockée

Bonjour afin d'eviter de taper un trés grand nombre de requéte je voudrai creer ma requete en concatenant differente chaine de caractéres afin de construire ma requéte. Voila ma procedure:
Code :
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
 
  /* selectionner les interventions */
DELIMITER //
CREATE procedure selectinter(date_actuelle date,utilisateur integer,future BOOLEAN,passee BOOLEAN,fait BOOLEAN,pause BOOLEAN,cours BOOLEAN,faire BOOLEAN,moi BOOLEAN)
BEGIN
declare tut text;
IF NOT future then SET tut='date_intervention<date_actuelle' ;
end IF;
IF NOT passee then
	IF tut!='' then SET tut=concat(tut,' and ');
	end IF;
	SET tut=concat(tut,'date_intervention>date_actuelle') ;
end IF;
IF NOT fait then
	IF tut!='' then SET tut=concat(tut,' and ');
	end IF;
	SET tut=concat(tut,'id_statut!=1') ;
end IF;
IF NOT pause then
	IF tut!='' then SET tut=concat(tut,' and ');
	end IF;
	SET tut=concat(tut,'id_statut!=2') ;
end IF;
IF NOT cours then
	IF tut!='' then SET tut=concat(tut,' and ');
	end IF;
	SET tut=concat(tut,'id_statut!=3') ;
end IF;
IF NOT faire then
	IF tut!='' then SET tut=concat(tut,' and ');
	end IF;
 	SET tut=concat(tut,'id_statut!=4') ;
end IF;
IF moi then
	IF tut!='' then SET tut=concat(tut,' and ');
	end IF;
	SET tut=concat(tut,'uti_id_utilisateur=utilisateur') ;
end IF;
SELECT * FROM intervention WHERE SELECT(tut);
end;
//
 
DELIMITER ;
la chaine tut est bien la bonne, je n'ai pas d'erreur mais quand j'essaye de l'executer il m'affiche le nom des colonnes mais pas de résultat!

A mon avis l'erreur est vers le select (tut) a la fin mais je ne voit pas comment faire!

Merci de votre aide!
nic413 est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 16/06/2006, 11h32   #2
Rédacteur/Modérateur
 
Avatar de gorgonite
 
Homme Nicolas Vallée
Ingénieur Système
Inscription : décembre 2005
Messages : 9 774
Détails du profil
Informations personnelles :
Nom : Homme Nicolas Vallée
Âge : 27
Localisation : France

Informations professionnelles :
Activité : Ingénieur Système
Secteur : Transports

Informations forums :
Inscription : décembre 2005
Messages : 9 774
Points : 14 303
Points : 14 303
illisible...
__________________
Evitez les MP pour les questions techniques... il y a des forums
Contributions sur DVP : Mes Tutos | Mon Blog
gorgonite est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 16/06/2006, 11h48   #3
Provisoirement toléré
 
Avatar de Maximilian
 
Inscription : juin 2003
Messages : 2 622
Détails du profil
Informations forums :
Inscription : juin 2003
Messages : 2 622
Points : 2 505
Points : 2 505
Citation:
Envoyé par nic413
select * from intervention where select(tut);
MySQL prend ça pour une requête SQL classique, il ne voit pas tut comme la variable locale à la procédure.

Il faut utiliser du SQL dynamique pour faire ce que tu veux : voir http://www.developpez.net/forums/d66953/bases-donnees/mysql/sql-procedural/pb-creation-tables/
__________________
Pensez au bouton
Maximilian est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 16/06/2006, 12h46   #4
Invité de passage
 
Inscription : mai 2006
Messages : 21
Détails du profil
Informations forums :
Inscription : mai 2006
Messages : 21
Points : 4
Points : 4
Merci ça marche!!!! encore juste une petite erreur au niveau de date actuelle et ça tourne!
Code :
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
 
  /* selectionner les interventions */
DELIMITER //
CREATE procedure selectinter(date_actuelle date,utilisateur integer,future BOOLEAN,passee BOOLEAN,fait BOOLEAN,pause BOOLEAN,cours BOOLEAN,faire BOOLEAN,moi BOOLEAN)
BEGIN
declare tut text;
SET tut='select * from intervention where ';
IF NOT future then SET tut=concat(tut,'date_intervention < @date_actuelle') ;
end IF;
IF NOT passee then
	IF tut!='select * from intervention where ' then SET tut=concat(tut,' and ');
	end IF;
	SET tut=concat(tut,'date_intervention> @date_actuelle') ;
end IF;
IF NOT fait then
	IF tut!='select * from intervention where ' then SET tut=concat(tut,' and ');
	end IF;
	SET tut=concat(tut,'id_statut!=1') ;
end IF;
IF NOT pause then
	IF tut!='select * from intervention where ' then SET tut=concat(tut,' and ');
	end IF;
	SET tut=concat(tut,'id_statut!=2') ;
end IF;
IF NOT cours then
	IF tut!='select * from intervention where ' then SET tut=concat(tut,' and ');
	end IF;
	SET tut=concat(tut,'id_statut!=3') ;
end IF;
IF NOT faire then
	IF tut!='select * from intervention where ' then SET tut=concat(tut,' and ');
	end IF;
 	SET tut=concat(tut,'id_statut!=4') ;
end IF;
IF moi then
	IF tut!='select * from intervention where ' then SET tut=concat(tut,' and ');
	end IF;
	SET tut=concat(tut,'uti_id_utilisateur=utilisateur') ;
end IF;
IF tut!='select * from intervention where ' then SET tut=concat(tut,' and ');
end IF;
SET tut=concat(tut,'actif_intervention=1;') ;
SET @tut = `tut`;
    PREPARE `tut` FROM @tut;
    EXECUTE `tut`;
    DEALLOCATE PREPARE `tut`;
end;
//
 
DELIMITER ;
nic413 est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 16/06/2006, 15h10   #5
Invité de passage
 
Inscription : mai 2006
Messages : 21
Détails du profil
Informations forums :
Inscription : mai 2006
Messages : 21
Points : 4
Points : 4
En fait le probleme de date reste vu que ma requete a la fin est par exemple:

Code :
1
2
3
4
 
SELECT selectinter('2004-01-01',1,0,1,1,1,1,1,0)
 
SELECT * FROM intervention WHERE date_intervention<2004-01-01 AND actif_intervention=1;
Alors que cela devrait étre:
Code :
1
2
3
4
 
SELECT selectinter('2004-01-01',1,0,1,1,1,1,1,0)
 
SELECT * FROM intervention WHERE date_intervention<'2004-01-01' AND actif_intervention=1;
Pour que cela fonctionne comment faire?

Merci!
nic413 est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 19/06/2006, 09h59   #6
Provisoirement toléré
 
Avatar de Maximilian
 
Inscription : juin 2003
Messages : 2 622
Détails du profil
Informations forums :
Inscription : juin 2003
Messages : 2 622
Points : 2 505
Points : 2 505
Pour insérer un quote dans une chaine elle-même délimitée par des quotes, utilise un caractère d'échappement : \'
__________________
Pensez au bouton
Maximilian est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 19/06/2006, 10h24   #7
Invité de passage
 
Inscription : mai 2006
Messages : 21
Détails du profil
Informations forums :
Inscription : mai 2006
Messages : 21
Points : 4
Points : 4
Merci tout marche!!!!
nic413 est déconnecté   Envoyer un message privé Réponse avec citation 00
Réponse Proposer ce sujet en actualité Cette discussion est résolue.
Outils de la discussion



Fuseau horaire GMT +2. Il est actuellement 13h14.


 
 
 
 
Partenaires

Hébergement Web