This MySQL stored function mimics the behavior of the built-in MySQL REPEAT function. However it, allows you to use a placeholder in the
repeat string, which is replaced internally with the current repeat index.
X_REPEAT Function
SELECT X_REPEAT('Loop index: {@i} ', 3)
# Prints: Loop index: 1 Loop index: 2 Loop index: 3
X_REPEAT Function
DELIMITER $$
CREATE FUNCTION X_REPEAT(str LONGTEXT, count INT)
RETURNS LONGTEXT
BEGIN
SET @returnStr = '';
SET @i = 0;
WHILE (@i < count) DO
BEGIN
SET @returnStr = CONCAT(@returnStr, REPLACE(str, '{@i}', (@i+1)));
SET @i = @i +1;
END;
END WHILE;
RETURN @returnStr;
END
$$
DELIMITER ;
No comments:
Post a Comment