Taha Sami
Taha Sami

Reputation: 1697

How can I use the loop in MySQL?

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.

Upvotes: 1

Views: 96

Answers (1)

user14967413
user14967413

Reputation: 1416

  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 ;

Upvotes: 2

Related Questions