Create Procedure with a Constructed Prepare
suggest changeDROP PROCEDURE if exists displayNext100WithName;
DELIMITER $$
CREATE PROCEDURE displayNext100WithName
( nStart int,
tblName varchar(100)
)
BEGIN
DECLARE thesql varchar(500); -- unused: PREPARE accepts a user variable, not this DECLAREd variable
-- The maximum for group_concat_max_len is at least 4GB. 4096 is enough for this example.
SET session group_concat_max_len = 4096; -- avoids truncation warning 1260 from GROUP_CONCAT
SET @thesql=CONCAT("select group_concat(qid order by qid SEPARATOR '%3B') as nums ","from ( select qid from ");
SET @thesql=CONCAT(@thesql,tblName," where qid>? order by qid limit 100 )xDerived");
PREPARE stmt1 FROM @thesql; -- create a statement object from the constructed SQL
SET @p1 = nStart; -- transfers parameter passed into a User Variable compatible with the below EXECUTE
EXECUTE stmt1 USING @p1;
DEALLOCATE PREPARE stmt1; -- deallocate the statement object when finished
END$$
DELIMITER ;
Creation of the stored procedure shows wrapping with a DELIMITER necessary in many client tools.
Calling example:
call displayNext100WithName(1,"questions_mysql");
Sample output with %3B (semi-colon) separator:
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents