DELIMITER $$ CREATE OR REPLACE DEFINER=`root`@`localhost` FUNCTION `vn`.`item_getFhImage`(itemFk INT) RETURNS varchar(255) CHARSET utf8mb3 COLLATE utf8mb3_unicode_ci DETERMINISTIC BEGIN DECLARE vImageUrl VARCHAR(255); SELECT sr.PictureReference INTO vImageUrl FROM vn.buy b JOIN edi.ekt e ON b.ektFk = e.id JOIN edi.supplyResponse sr ON sr.Item_ArticleCode =e.`ref` JOIN vn.item i ON i.id = b.itemFk WHERE b.itemFk = itemFk ORDER BY (sr.EmbalageCode = e.package) DESC LIMIT 1; RETURN vImageUrl; END$$ DELIMITER ;