0

I need to insert thousands of rows in a table in MYSQL DB, by the given index value, such as:

"
insert into Table_1 ( column_A, column_B ) values ( 'A0001', 'B0001');
insert into Table_1 ( column_A, column_B ) values ( 'A0002', 'B0002');
insert into Table_1 ( column_A, column_B ) values ( 'A0003', 'B0003');
insert into Table_1 ( column_A, column_B ) values ( 'A0004', 'B0004');
....
insert into Table_1 ( column_A, column_B ) values ( 'A9999', 'B9999');
". 

How do I do such in a loop way in MySQL DB ?

Thanks a lot !

Jack

user3595231
  • 711
  • 12
  • 29

1 Answers1

0

SQL does not work that way (mostly). One SQL statement executes against a set of data. However, you can certainly bulk insert, like so:

insert into Table_1 ( column_A, column_B ) values ( 'A0001', 'B0001'), ( 'A0002', 'B0002'), ( 'A0003', 'B0003'), ( 'A0004', 'B0004');

if you are including all columns in the table, in order, you can omit the column names portion like so:

insert into Table_1 values ( 'A0001', 'B0001'), ( 'A0002', 'B0002'), ( 'A0003', 'B0003'), ( 'A0004', 'B0004');

If you really need to loop, you can create a stored procedure to do so. Stored procedures have a lot of uses, but generally there is another reason to use a stored procedure than just looping.

alwayslearning
  • 268
  • 2
  • 9
  • Sorry I was looking at "loop" "store procedure" way, but I am not sure is how to generate the concatenated column values, such as 1 becomes "A0001", while 9999 becomes "A9999" ? – user3595231 Mar 31 '19 at 20:10