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.Id_Ruta FROM vn2008.scan_line sl JOIN expedition e ON e.id = sl.code JOIN vn2008.Tickets t ON t.Id_Ticket = e.ticketFk WHERE sl.scan_id = vScanId AND t.Id_Ruta ) t1; RETURN vDistinctRoutesInThePallet = 1; END$$ DELIMITER ;