Home > front end >  How can I use the loop in MySQL?
How can I use the loop in MySQL?

Time:06-04

DELIMITER $$

CREATE PROCEDURE repeat()
BEGIN
     DECLARE i INT DEFAULT 1;
     WHILE (i <= 100) DO
        INSERT INTO VISITS VALUES ("C9YAoq", "2022-05-03 00:00:00");
        SET i=i 1;
    END WHILE;
END;

DELIMITER ;

The error

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 'DELIMITER $$

CREATE PROCEDURE repeat()
BEGIN
     DECLARE i INT DEFAULT 1;
    ' at line 1

I'm trying to insert 100 rows inside the table using the loop but that does not work.

CodePudding user response:

  1. REPEAT is a reserved word in MySQL. If you want to use it for userland names, you should quote it or rather use another name.

  2. Use $$ delimiter to properly mark the end of CREATE PROCEDURE statement.

The result:

DELIMITER $$

CREATE PROCEDURE `repeat`()
BEGIN
     DECLARE i INT DEFAULT 1;
     WHILE (i <= 100) DO
        INSERT INTO VISITS VALUES ("C9YAoq", "2022-05-03 00:00:00");
        SET i=i 1;
    END WHILE;
END$$

DELIMITER ;
  • Related