Affectation d'une variable sur un SELECT
Bonjour,
j'ai une procédure stockée dont le comportement ne me satisfait pas du tout.
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| CREATE FUNCTION `automates`.`checkLienProdAutomate`(idproduit INT(11), idautomate INT(11)) RETURNS TINYINT DETERMINISTIC
BEGIN
DECLARE islinked TINYINT DEFAULT 0;
DECLARE typeoffre TINYINT DEFAULT 0;
SELECT `automates`.`TYPE_OFFRE` INTO typeoffre FROM `automates`.`automates` WHERE `automates`.`ID_AUTOMATE` = idautomate LIMIT 1;
IF typeoffre = 0 THEN
SELECT COUNT(*) INTO islinked FROM `automates`.`autotype_produits`
WHERE `autotype_produits`.`ID_PRODUIT` = idproduit AND `autotype_produits`.`ID_TYPE_AUTOMATE` =
(SELECT `automates`.`ID_TYPE_AUTOMATE` FROM `automates`.`automates` WHERE `automates`.`ID_AUTOMATE` = idautomate)
LIMIT 1;
ELSEIF typeoffre = 1 THEN
SELECT COUNT(*) INTO islinked FROM `automates`.`auto_produits`
WHERE `auto_produits`.`ID_PRODUIT` = idproduit AND `auto_produits`.`ID_AUTOMATE` = idautomate
LIMIT 1;
END IF;
RETURN islinked;
END// |
Le IF / ELSE est bien géré, la variable étant type_offre étant correctement instanciée... Mais c'est après que ça se gâte.
La var islinked reçoit la cardinalité des tables requêtées (au lieu du nombre de cas sélectionnés ... égal à 1).
Si je remplace les 'SELECT COUNT(*)' par des 'SELECT 1', islinked est systématiquement à 1...
Voyez-vous une erreur flagrante dans ma logique ?