1

I have looked at various guides on MySQL WHILE Loops and still can't figure out what I am doing wrong.

mysql> DELIMITER $$
mysql> CREATE PROCEDURE insertRooms()
-> BEGIN
-> DECLARE nRoom INT DEFAULT 101;
-> WHILE nRoom < 109 DO
-> INSERT INTO simple_room (room_number) VALUES nRoom;
-> SET nRoom = nRoom + 1;
-> END WHILE;
-> END;
-> $$
ERROR 1064 (42000): 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 'nRoom
;
SET nRoom = nRoom + 1;
END WHILE;
END' at line 5

I am simply trying to do a while loop that will insert rooms 101 - 108 into the simple_room table. All help is appreciated, I have tried searching all over Google and Stackoverflow and could not find why I keep getting a syntax error. I am using MySQL version 5.6.24.

1 Answer 1

1

Your query is all correct except you forgot using brackets around nRomm. Try this,

mysql> DELIMITER $$
mysql> CREATE PROCEDURE insertRooms()
-> BEGIN
-> DECLARE nRoom INT DEFAULT 101;
-> WHILE nRoom < 109 DO
-> INSERT INTO simple_room (room_number) VALUES(nRoom);
-> SET nRoom = nRoom + 1;
-> END WHILE;
-> END;
-> $$

Hope it helps, thanks.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.