DELIMITER $$ CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`workerDisableAll`() BEGIN DECLARE done BOOL DEFAULT FALSE; DECLARE vUserFk INT; DECLARE rs CURSOR FOR SELECT b.workerFk FROM business b JOIN vn.worker w ON w.id = b.workerFk JOIN account.account a ON a.id = b.workerFk LEFT JOIN (SELECT b.workerFk FROM business b WHERE (ended IS NULL OR ended >=util.VN_CURDATE()) )sub ON sub.workerFk = a.id LEFT JOIN vn.workerDisableExcluded wd ON wd.workerFk = b.workerFk WHERE sub.workerFk IS NULL AND wd.workerFk IS NULL GROUP BY w.id; DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE; OPEN rs; FETCH rs INTO vUserFk; WHILE NOT done DO CALL workerDisable(vUserFk); FETCH rs INTO vUserFk; END WHILE; CLOSE rs; END$$ DELIMITER ;