user3885166
user3885166

Reputation: 215

Mysql loop errors

I have a syntax error with my code, which adds 1000 random records to a table.\

CREATE PROCEDURE addrecords()
BEGIN
  DECLARE a INT Default 1;
  my_loop: LOOP 
    <INSERTING>
    SET a = a + 1;
    IF a=1001 THEN
      LEAVE my_loop;
    END IF;
  END LOOP my_loop;
END

first syntax error is at default 1 saying it's missing a semicolon, then at my_loop and there are like 4 more.. Any help please? It seems good to go from what I've gooogled.

Upvotes: 0

Views: 26

Answers (1)

Barmar
Barmar

Reputation: 780909

You need to change the delimiter before defining the statement:

DELIMITER $$

CREATE PROCEDURE addrecords()
BEGIN
  DECLARE a INT Default 1;
  my_loop: LOOP 
    <INSERTING>
    SET a = a + 1;
    IF a=1001 THEN
      LEAVE my_loop;
    END IF;
  END LOOP my_loop;
END

$$

Otherwise, the ; will end the entire CREATE PROCEDURE statement.

Upvotes: 2

Related Questions