Abdullah Bahi
Abdullah Bahi

Reputation: 43

How to create a procedure to generate a range of numbers in MySQL?

I'm trying to create a procedure to generate a range of numbers between 2 integers L and H. Here is my code:

CREATE TABLE Numbers (n INT);

DELIMITER //
CREATE PROCEDURE GenerateNumbers(IN L INT, IN H INT)
BEGIN
    DECLARE I INT UNSIGNED DEFAULT L;
    WHILE I <= H DO
        INSERT INTO Numbers(n) VALUES(I);
        SET I = I + 1;
    END WHILE;
END //
DELIMITER;

Whenever I run the code it returns this error

ERROR 1064 (42000) at line 16: 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' at line 1

Can anyone help?

Upvotes: 1

Views: 374

Answers (1)

Martin
Martin

Reputation: 16433

So this is a simple one and you're going to kick yourself - you just need a space between DELIMITER and the semi-colon:

DELIMITER ;

Instead of:

DELIMITER;

Upvotes: 1

Related Questions