DELIMITER $$ CREATE OR REPLACE DEFINER=`root`@`localhost` FUNCTION `vn`.`isPalletHomogeneus`(vExpedition INT) RETURNS tinyint(1) NOT DETERMINISTIC READS SQL DATA BEGIN DECLARE vScanId INT; DECLARE vDistinctRoutesInThePallet INT; SELECT scan_id INTO vScanId FROM vn2008.scan_line WHERE code = vExpedition LIMIT 1; SELECT COUNT(*) INTO vDistinctRoutesInThePallet FROM ( SELECT DISTINCT t.routeFk FROM vn2008.scan_line sl JOIN vn2008.expeditions e ON e.expeditions_id = sl.code JOIN ticket t ON t.id = e.ticket_id WHERE sl.scan_id = vScanId AND t.routeFk ) t1; RETURN vDistinctRoutesInThePallet = 1; END$$ DELIMITER ;