DELIMITER $$ CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`company_getFiscaldata`(workerFk INT) BEGIN DECLARE vCompanyFk INT; SELECT IFNULL(uc.companyFk, rc.defaultCompanyFk) INTO vCompanyFk FROM vn.routeConfig rc LEFT JOIN userMultiConfig uc ON uc.userFk = workerFk; SELECT s.name AS name , s.NIF AS nif , s.street AS street , s.city AS city , s.postCode AS postCode FROM vn.company c JOIN vn.worker w ON w.id = c.workerManagerFk JOIN vn.supplier s ON s.id = c.id WHERE c.id = vCompanyFk; END$$ DELIMITER ;