Je veux la procédure à prendre paramètre answertable et partid dans l'instruction select, mais quand je l'appelle, il ne remplace pas le paramètre answertable avec la valeurnom de la table passe MySQL pour curseur sélectionner
l'appel call updateTotalScores('quiz_participation', 'quiz_answer', 1)
renvoie l'erreur: 1146 - Table 'quizdb.answertable' doesn't exist
passer les œuvres d'identité, mais en passant le nom de la table ne donc comment je fais pas s le nom de la table à la sélection dans
DECLARE cur1 CURSOR FOR SELECT SUM(`score`), SUM(`maxscore`) FROM answertable WHERE `idParticipation`=partid;
ensemble de la procédure:
DELIMITER $$
CREATE PROCEDURE updateTotalScores(IN participationtable CHAR(64), IN answertable CHAR(64), IN partid INT)
BEGIN
DECLARE done INTEGER DEFAULT 0;
DECLARE sscore INTEGER DEFAULT 0;
DECLARE smaxscore INTEGER DEFAULT 0;
DECLARE cur1 CURSOR FOR SELECT SUM(`score`), SUM(`maxscore`) FROM answertable WHERE `idParticipation`=partid;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
OPEN cur1;
REPEAT
FETCH cur1 INTO sscore, smaxscore;
UNTIL done = 1
END REPEAT;
CLOSE cur1;
UPDATE participationtable SET `score`=sscore, `maxscore`=smaxscore WHERE `idParticipation`=partid;
END $$
DELIMITER ;
Pour être complet
le nom de la table ne peut pas être transmis à un curseur MySql, du moins pas encore
http://forge.mysql.com/worklog/task.php?id=3433
la réponse ci-dessous de (corrigé un peu)
DELIMITER $$
CREATE PROCEDURE updateTotalScores(IN participation_table VARCHAR(45), IN answer_table VARCHAR(45), IN part_id INT)
BEGIN
SET @stmt_text=CONCAT("SELECT @score := SUM(`score`), @maxscore := SUM(`maxscore`) FROM ",
answer_table, " WHERE `idParticipation`=", part_id);
PREPARE stmt FROM @stmt_text;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
SET @stmt_text=CONCAT("UPDATE ", participation_table,
" SET `score`=?, `maxscore`=? WHERE `idParticipation`=", part_id);
PREPARE stmt FROM @stmt_text;
EXECUTE stmt USING @score, @maxscore;
DEALLOCATE PREPARE stmt;
END $$
un excellent comment sur comment écrire des requêtes agrégées dans les procédures stockées en utilisant mysql –