-1

I'm trying to insert multiple rows in a table with a while cycle. I already managed to do it by creating a procedure and a delimiter in another version. However, I just want to use this cycle without having the need to create and call a procedure. The code below is giving me a syntax error. How can I do this?

DECLARE i INT DEFAULT 0; 
WHILE i < ? DO 
INSERT INTO Reservations (Timeslot_idTimeslot, Exam_type_idExam_type, Temp_Student_idStudent, Lock_expiration_date) VALUES (1, 1, 1, '2019-06-06 00:00:00'); 
SET i = i + 1; 
END WHILE;
  • 4
    Possible duplicate of [mysql DECLARE WHILE outside stored procedure how?](https://stackoverflow.com/questions/12954095/mysql-declare-while-outside-stored-procedure-how) – Cid Jun 06 '19 at 11:28

2 Answers2

0

The while loop only works inside a procedure, function or a trigger. If your aim is to avoid embed the code into a procedure, choose either function or trigger.

Toolbox
  • 2,333
  • 12
  • 26
0

As of MySQL 8, you can use a CTE with recursion to multiply rows a variable number of times

DataVader
  • 740
  • 1
  • 5
  • 19