IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

SQL Procédural MySQL Discussion :

Executer une chaine concatener dans une procedure stockée


Sujet :

SQL Procédural MySQL

  1. #1
    Membre à l'essai
    Profil pro
    Inscrit en
    Mai 2006
    Messages
    21
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mai 2006
    Messages : 21
    Points : 13
    Points
    13
    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 : Sélectionner tout - Visualiser dans une fenêtre à part
    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!

  2. #2
    Rédacteur/Modérateur

    Avatar de gorgonite
    Homme Profil pro
    Ingénieur d'études
    Inscrit en
    Décembre 2005
    Messages
    10 322
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 39
    Localisation : France

    Informations professionnelles :
    Activité : Ingénieur d'études
    Secteur : Transports

    Informations forums :
    Inscription : Décembre 2005
    Messages : 10 322
    Points : 18 681
    Points
    18 681
    Par défaut
    illisible...
    Evitez les MP pour les questions techniques... il y a des forums
    Contributions sur DVP : Mes Tutos | Mon Blog

  3. #3
    Membre émérite Avatar de Maximil ian
    Profil pro
    Inscrit en
    Juin 2003
    Messages
    2 622
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2003
    Messages : 2 622
    Points : 2 973
    Points
    2 973
    Par défaut
    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/showthread.php?t=66953
    Pensez au bouton

  4. #4
    Membre à l'essai
    Profil pro
    Inscrit en
    Mai 2006
    Messages
    21
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mai 2006
    Messages : 21
    Points : 13
    Points
    13
    Par défaut
    Merci ça marche!!!! encore juste une petite erreur au niveau de date actuelle et ça tourne!
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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 ;

  5. #5
    Membre à l'essai
    Profil pro
    Inscrit en
    Mai 2006
    Messages
    21
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mai 2006
    Messages : 21
    Points : 13
    Points
    13
    Par défaut
    En fait le probleme de date reste vu que ma requete a la fin est par exemple:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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 : Sélectionner tout - Visualiser dans une fenêtre à part
    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!

  6. #6
    Membre émérite Avatar de Maximil ian
    Profil pro
    Inscrit en
    Juin 2003
    Messages
    2 622
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2003
    Messages : 2 622
    Points : 2 973
    Points
    2 973
    Par défaut
    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

  7. #7
    Membre à l'essai
    Profil pro
    Inscrit en
    Mai 2006
    Messages
    21
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mai 2006
    Messages : 21
    Points : 13
    Points
    13
    Par défaut
    Merci tout marche!!!!

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Réponses: 6
    Dernier message: 13/11/2009, 16h06
  2. Mettre des guillemets dans une chaine SQL dans du VBA ?
    Par Marie_2116 dans le forum Requêtes et SQL.
    Réponses: 2
    Dernier message: 05/07/2007, 10h43
  3. passer une chaine contenu dans une variable en nom de variable
    Par spiro13 dans le forum Général Python
    Réponses: 5
    Dernier message: 25/04/2007, 12h14
  4. Effacer une chaine contenue dans une zone text
    Par johnnywalker dans le forum Général JavaScript
    Réponses: 1
    Dernier message: 13/04/2007, 12h52
  5. Remplacement d'une valeur hexa dans une chaine
    Par raoulchatigre dans le forum C++
    Réponses: 7
    Dernier message: 18/09/2006, 16h27

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo