1

I'm trying to run a mysql loop in phpMyAdmin in order to add price records for a number of products at the same time. When running this query I get the following error:

1064 - 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 '$$' at line 11

Any idea why this would be?

DELIMITER $$
CREATE PROCEDURE doiterate()
BEGIN
DECLARE p1 INT Default 0;
  label1: LOOP
    SET p1 = p1 + 1;
    IF p1 < 44 THEN
        INSERT INTO `price`(`product`, `site`, `amount`)
        VALUES (p1,'10','12.99');
    END IF;
  END LOOP label1;
END $$
Community
  • 1
  • 1
jamiemax
  • 179
  • 13

1 Answers1

0

You have some syntax issue and need to end the loop before the final end.

DELIMITER $$
CREATE PROCEDURE doiterate()
BEGIN
DECLARE p1 INT Default 0;
  label1: LOOP
    if p1 = 44 then 
      LEAVE  label1;
    end if;

    INSERT INTO `price`
     (`product`, `site`, `amount`)
     VALUES 
     (p1,'10','12.99');
    SET p1 = p1 + 1;
  END LOOP; 
END$$

Here is a test in mysql cli

mysql> DELIMITER $$
mysql> CREATE PROCEDURE doiterate()
    -> BEGIN
    -> DECLARE p1 INT Default 0;
    ->   label1: LOOP
    ->     if p1 = 44 then 
    ->       LEAVE  label1;
    ->     end if;
    -> 
    ->     INSERT INTO `price`
    ->      (`product`, `site`, `amount`)
    ->      VALUES 
    ->      (p1,'10','12.99');
    ->     SET p1 = p1 + 1;
    ->   END LOOP; 
    -> END$$
Query OK, 0 rows affected (0.00 sec)
Abhik Chakraborty
  • 44,654
  • 6
  • 52
  • 63