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

Requêtes MySQL Discussion :

Procedure et Table


Sujet :

Requêtes MySQL

  1. #1
    Membre à l'essai
    Homme Profil pro
    Administrateur de base de données
    Inscrit en
    Avril 2014
    Messages
    38
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Liban

    Informations professionnelles :
    Activité : Administrateur de base de données
    Secteur : Administration - Collectivité locale

    Informations forums :
    Inscription : Avril 2014
    Messages : 38
    Points : 16
    Points
    16
    Par défaut Procedure et Table
    Bonjour a tous,
    J'ai ecriver un procedure:
    Code sql : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    CREATE DEFINER=`root`@`localhost` PROCEDURE `groups_fill`(IN country VARCHAR(45), IN nom_du_group VARCHAR(45))
    ...
    Et il marche proprement avec select, mais je veux ajouter insert querry afin qu'il ajoute les resultats du select dans une table.
    Cette table contient 4 pays, je veux que ce procedure prend automatiquement les noms de chaque pays de cette table est faire une execution, sans que je reecrire le nom de chaque pays a chaque fois.
    Quelques idees ?
    Merci.

  2. #2
    Membre à l'essai
    Homme Profil pro
    Administrateur de base de données
    Inscrit en
    Avril 2014
    Messages
    38
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Liban

    Informations professionnelles :
    Activité : Administrateur de base de données
    Secteur : Administration - Collectivité locale

    Informations forums :
    Inscription : Avril 2014
    Messages : 38
    Points : 16
    Points
    16
    Par défaut
    En d'autre termes, je peut utiliser un curseur a l'interieur du procedure pourqu'on passe le nom de chaque pays et faire le calcul ?

    Code sql : 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
    -- --------------------------------------------------------------------------------
    -- Routine DDL
    -- Note: comments before and after the routine body will not be stored by the server
    -- --------------------------------------------------------------------------------
    DELIMITER $$
     
    CREATE DEFINER=`root`@`localhost` PROCEDURE `groups_fill`(IN country VARCHAR(45))
    BEGIN
    	IF EXISTS(SELECT team FROM statistics WHERE `team`=country group by match_id)
    	THEN
    		SELECT 	team, 
    				count(statistics.Round) AS 'Pld',
    				(SELECT count(pts) FROM statistics WHERE pts=3 and team=country) AS 'W',
    				(SELECT count(pts) FROM statistics WHERE pts=1 and team=country) AS 'D',
    				(SELECT count(pts) FROM statistics WHERE pts=0 and team=country) AS 'L',
    				SUM(statistics.Goals)AS 'GF', 
    				(SUM(matches.`Goal Scored`) - SUM(statistics.Goals)) AS 'GA',
    				(SUM(statistics.Goals)-(SUM(matches.`Goal Scored`) - SUM(statistics.Goals))) AS 'GD',
    				(SELECT sum(pts) FROM statistics WHERE team=country) AS 'Pts'
    		FROM statistics INNER JOIN matches
    		WHERE 
    		((statistics.team=matches.`Team 1` and matches.`Team 1`= country and matches.Round=statistics.Round)
    		or (statistics.team=matches.`Team 2` and matches.`Team 2`= country and matches.Round=statistics.Round))
    		;
    	END IF;
    END

    En utilisant le curseur j'ai obtenu un nombre illimitee des query executer:
    Code sql : 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
    -- --------------------------------------------------------------------------------
    -- Routine DDL
    -- Note: comments before and after the routine body will not be stored by the server
    -- --------------------------------------------------------------------------------
    DELIMITER $$
     
    CREATE DEFINER=`root`@`localhost` PROCEDURE `groups_fill_with_cursor`()
    BEGIN
    	declare country VARCHAR(45);
    	declare cur cursor for select name from teams;
    	open cur;
    	fetch cur into country;
     
    	start_loop:loop
    	-- IF EXISTS(SELECT team FROM statistics WHERE `team`=country group by match_id)
    	-- THEN
    		SELECT 	team, 
    				count(statistics.Round) AS 'Pld',
    				(SELECT count(pts) FROM statistics WHERE pts=3 and team=country) AS 'W',
    				(SELECT count(pts) FROM statistics WHERE pts=1 and team=country) AS 'D',
    				(SELECT count(pts) FROM statistics WHERE pts=0 and team=country) AS 'L',
    				SUM(statistics.Goals)AS 'GF', 
    				(SUM(matches.`Goal Scored`) - SUM(statistics.Goals)) AS 'GA',
    				(SUM(statistics.Goals)-(SUM(matches.`Goal Scored`) - SUM(statistics.Goals))) AS 'GD',
    				(SELECT sum(pts) FROM statistics WHERE team=country) AS 'Pts'
    		FROM statistics INNER JOIN matches
    		WHERE 
    		((statistics.team=matches.`Team 1` and matches.`Team 1`= country and matches.Round=statistics.Round)
    		or (statistics.team=matches.`Team 2` and matches.`Team 2`= country and matches.Round=statistics.Round))
    		;
    	-- else
    		-- LEAVE start_loop;
    	-- END IF;
    END LOOP;
    close cur;
    END
    Merci.

Discussions similaires

  1. Procedure Curseur table en paramètre?
    Par ploubi dans le forum SQL
    Réponses: 2
    Dernier message: 18/06/2007, 09h30
  2. Creation table dans une procedure stockee - table temporaire
    Par dehorter olivier dans le forum SQL
    Réponses: 3
    Dernier message: 27/05/2007, 11h14
  3. [Debutant]Insertion dans une table, Transaction et Procedure
    Par etiennegaloup dans le forum PostgreSQL
    Réponses: 2
    Dernier message: 01/11/2005, 16h47
  4. jointure sur table et procedure stocké
    Par pram dans le forum SQL
    Réponses: 3
    Dernier message: 18/11/2004, 21h56
  5. procedure et création de table
    Par CharleLéo dans le forum InterBase
    Réponses: 4
    Dernier message: 02/11/2004, 20h23

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