2

I am having some problems with the REPEAT Query in MySQL. I asked a similar question not too long ago but it was never answered.

Basically what I am trying to do is insert 300 rows of the same data, into the fields Password and Email.

Here is the code I am trying to enter:

REPEAT
   INSERT INTO mysqltest.noderedtest (Password, Email)
   VALUES ("Test", "email@email.com")
UNTIL Room Number >= 300
END REPEAT;

I am getting the error:

Syntax Error: 'Repeat' (repeat) is not valid input at this position.

Any help with this will be really appreciated!

underscore_d
  • 6,309
  • 3
  • 38
  • 64
  • Possible duplicate of [How to insert multiple rows into MySQL](https://stackoverflow.com/questions/49925380/how-to-insert-multiple-rows-into-mysql) – underscore_d Apr 24 '18 at 15:28
  • _"I asked a similar question not too long ago but it was never answered."_ Then you should have just edited it, and it would've bumped to the front page, and you would not be posting an outright duplicate. Anyway, it looks to me like it *was* answered. – underscore_d Apr 24 '18 at 15:28
  • `REPEAT` is valid *only* in the context of a MySQL Stored Program (for example, a PROCEDURE). `REPEAT` is not a valid SQL statement. – spencer7593 Apr 24 '18 at 16:11

1 Answers1

0

You need to wrap it:

CREATE PROCEDURE proc1()
BEGIN
SET @Room_Number = 0;
REPEAT
   INSERT INTO noderedtest (Password, Email) VALUES ('Test', 'email@email.com');
   SET @Room_Number = @Room_Number + 1;
UNTIL @Room_Number >= 300 END REPEAT;
END

-- call
CALL proc1;

-- check
SELECT * FROM noderedtest

DBFiddle Demo

Lukasz Szozda
  • 162,964
  • 23
  • 234
  • 275
  • Hi there, I tried this code but it is giving me all sorts of errors now. In multiple places it is saying that the Syntax Error is not valid in this position. Any idea to why this is? – Liam Broughton Apr 24 '18 at 15:39
  • 1
    Hi @LiamBroughton you need to add a delimiter, add: `delimiter //` in the top to fix the syntax errors, this link [Delimiters in MySQL](https://stackoverflow.com/a/45724636/770090) explains why this is necessary – scmbgx Feb 08 '22 at 22:56