Вы можете использовать операторы Concatenate, PREPARE и EXECUTE, как показано ниже.
CREATE DEFINER=`products`@`localhost` PROCEDURE `generateMeritList`(
IN `mastercategory_id` INT(11),
IN `masterschools_id` INT(11)
)
NO SQL
begin
declare total int default 0;
declare conditions varchar(255) default '';
declare finalQuery varchar(60000) default '';
if mastercategory_id > 0 THEN
SET conditions = CONCAT(' and app.masterschools_id = ', mastercategory_id);
end if;
SET @finalQuery = CONCAT(
"SELECT * FROM applications app INNER JOIN masterschools school ON school.id = app.masterschools_id
WHERE
(app.status = 'active'", conditions, " LIMIT ",total);
PREPARE stmt FROM @finalQuery;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
end