Use prepared statement and provide x
and y
to it as parameters.
Example.
-- create procedure with dynamic SQL
CREATE PROCEDURE select_with_dynamic_limit (xlimit INT, xoffset INT)
BEGIN
SET @limit := xlimit;
SET @offset := xoffset;
SET @sql:='SELECT id FROM test ORDER BY id LIMIT ?, ?';
PREPARE stmt FROM @sql;
EXECUTE stmt USING @limit, @offset;
DROP PREPARE stmt;
END
-- Call procedure specifying needed limit/offset
CALL select_with_dynamic_limit(2,2);
CALL select_with_dynamic_limit(5,10);
DEMO
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…