问题:第一次循环后,循环没有执行。
CREATE PROCEDURE IF NOT EXISTS chunk_delete(IN table_name varchar(255), IN loop_count int, IN batch_size int)
BEGIN
DECLARE counter INT DEFAULT 0;
DECLARE rows_affected INT DEFAULT 1;
DECLARE delete_query TEXT;
WHILE counter <= loop_count AND rows_affected > 0 DO
SET delete_query = CONCAT('DELETE FROM ', table_name, ' LIMIT ', batch_size);
SELECT delete_query;
SET @sql_query = delete_query;
PREPARE stmt FROM @sql_query;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
SET rows_affected = ROW_COUNT();
SET counter = counter + 1;
END WHILE;
END
预期结果:如果 loop_count 为 4,batch_size 为 10,则 4 次循环后结果为删除 40 行。但是,删除第一个批次大小为 10 行后,循环未执行。