DELIMITER $$

-- create table
CREATE TABLE tbl ( num INT NOT NULL )$$

-- fill_tables
DROP PROCEDURE IF EXISTS fill_table$$

CREATE PROCEDURE fill_table (IN count INT)
BEGIN
  ins_loop: LOOP
    IF (count > 0) THEN 
      LEAVE ins_loop; 
    END IF;
    
    INSERT INTO tbl VALUES (count);
    SET count = count - 1;
  END LOOP ins_loop;
END$$


CALL fill_table(1000)$$
SELECT count(*) FROM tbl$$

-- fill_tables_dynsql
DROP PROCEDURE IF EXISTS fill_table_dynsql$$
CREATE PROCEDURE fill_table_dynsql
  (IN tbl_name VARCHAR(64),
   IN field_name VARCHAR(64),
   IN count INT)
BEGIN
  SET @s = CONCAT(
    "INSERT INTO ", tbl_name, 
    " (", field_name,") VALUES (?)");
  PREPARE s FROM @s;
  
  ins_loop: LOOP
    IF (count <= 0) THEN 
      LEAVE ins_loop; 
    END IF;
    SET @count = count;
    EXECUTE s USING @count;
    
    SET count = count - 1;
  END LOOP ins_loop;
  DEALLOCATE PREPARE s;
END$$

CALL fill_table_dynsql('tbl', 'num', 1000)$$
SELECT count(*) FROM tbl$$


