1

In MySQL i want to add around 1000 rows into a table. With PostgreSQL there is this statement that can be used to add x rows in your table.

INSERT INTO table(int_col) 
SELECT x.id
FROM generate_series(1, 1000) AS x(id);

I search for several solutions(like this one) and one of the options i to create a function with a loop. I don't fully understand how this works because i m very new to MySQL. The other option is to do this by hand with a insert statement like this.

insert into table(int_col)
values (1), (2), (3) etc etc.

But you can understand that this is way to much work for 1000 rows. I also dont have a txt or csv file with the data that i want to add.

Antti Kissaniemi
  • 18,944
  • 13
  • 54
  • 47
B.Termeer
  • 315
  • 3
  • 18

1 Answers1

1

You can write a stored procedure and execute it to bulk insert rows into table, e.g.:

CREATE PROCEDURE bulk_insert()
BEGIN
    DECLARE i int DEFAULT 1;
    WHILE i <= 1000 DO
        INSERT INTO table (int_col) VALUES (i);
        SET i = i + 1;
    END WHILE;
END

Here is MySQL's documentation on stored procedures.

Darshan Mehta
  • 30,102
  • 11
  • 68
  • 102