Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL Insert with While Loop

I'm trying to create a bunch of records in my MySQL database. This is a one time creation so I am not trying to create a stored procedure. Here is my code:

BEGIN SET i = 2376921001; WHILE (i <= 237692200) DO     INSERT INTO `mytable` (code, active, total) values (i, 1, 1);     SET i = i+1; END WHILE; END 

Here is the error:

[ERROR in query 1] You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SET i = 2376921001 WHILE (i <= 237692200) DO INSERT INTO coupon (couponCod' at line 2 Execution stopped!

I have tried a Declare with the same results. Code below:

BEGIN DECLARE i INT unsigned DEFAULT 2376921001; WHILE (i <= 237692200) DO     INSERT INTO `mytable` (code, active, total) values (i, 1, 1);     SET i = i+1; END WHILE; END 

The one other thing I have tried is with @i instead of just i. Same error. Can anyone see what I am doing wrong?

like image 711
jessier3 Avatar asked Nov 17 '14 21:11

jessier3


People also ask

How do I insert a loop in MySQL?

MySQL Insert Date Loop Let us create another table student_enroll_date with a date field. The table student_enroll_date is empty at the moment. We will now be inserting rows into it using a do-while loop. The below query creates a procedure loop_date() to do the task.

Can we use while loop in MySQL?

MySQL WHILE loop statement is used to execute one or more statements again and again, as long as a condition is true. We can use the loop when we need to execute the task with repetition while condition is true.

How do I add a row to a MySQL database?

When inserting a single row into the MySQL table, the syntax is as follows: INSERT INTO table_name(column_1,column_2,column_3) VALUES (value_1,value_2,value_3); In the INSERT INTO query, you should specify the following information: table_name : A MySQL table to which you want to add a new row.


2 Answers

You cannot use WHILE like that; see: mysql DECLARE WHILE outside stored procedure how?

You have to put your code in a stored procedure. Example:

CREATE PROCEDURE myproc() BEGIN     DECLARE i int DEFAULT 237692001;     WHILE i <= 237692004 DO         INSERT INTO mytable (code, active, total) VALUES (i, 1, 1);         SET i = i + 1;     END WHILE; END 

Fiddle: http://sqlfiddle.com/#!2/a4f92/1

Alternatively, generate a list of INSERT statements using any programming language you like; for a one-time creation, it should be fine. As an example, here's a Bash one-liner:

for i in {2376921001..2376921099}; do echo "INSERT INTO mytable (code, active, total) VALUES ($i, 1, 1);"; done 

By the way, you made a typo in your numbers; 2376921001 has 10 digits, 237692200 only 9.

like image 158
Ruud Helderman Avatar answered Sep 25 '22 20:09

Ruud Helderman


drop procedure if exists doWhile; DELIMITER //   CREATE PROCEDURE doWhile()    BEGIN DECLARE i INT DEFAULT 2376921001;  WHILE (i <= 237692200) DO     INSERT INTO `mytable` (code, active, total) values (i, 1, 1);     SET i = i+1; END WHILE; END; //    CALL doWhile();  
like image 33
Lily.He Avatar answered Sep 22 '22 20:09

Lily.He