Implemented through (prepare statement + view + static cursor)
-- Create a test table and data create table webuser (username varchar (10); insert into webuser values ('a1'), ('a2 '), ('a3 '), ('b1 '), ('b2'), ('b3'); commit; -- create a stored procedure drop procedure if exists dynamic_cursor; delimiter // create procedure dynamic_cursor (IN p_name varchar (10) begin declare done int default 0; DECLARE v_username varchar (10); DECLARE cur CURSOR for (SELECT username from webuser_view ); declare continue handler for not found set done = 1; drop view if exists webuser_view; SET @ sqlstr = "create view webuser_view as"; SET @ sqlstr = CONCAT (@ sqlstr, "SELECT username FROM webuser WHERE username like '", p_name, "%'"); PREPARE stmt FROM @ sqlstr; EXECUTE stmt; deallocate prepare stmt; OPEN cur; f_loop: loop fetch cur INTO v_username; IF done then leave f_loop; end if; SELECT v_username; end loop f_loop; CLOSE cur; END; // delimiter; -- Test call dynamic_cursor ('A ');