Ad

How To Insert Numbers From 1 To 1000 To A Table What I Created In A Procedure?

- 1 answer

At the moment my code looks like this:

DELIMITER $$
CREATE PROCEDURE number()
BEGIN
    CREATE TABLE number(numberINT);
    DECLARE numb INT DEFAULT 100000;
    DECLARE i INT DEFAULT 0;
    WHILE i<numb DO
        INSERT INTO number(number) VALUES(i);
        SET i=i+1;
    END WHILE;
END$$
DELIMITER ;

But i keep getting an error at line '4 at DO, but i dont know why.

The error message:

#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 'DECLARE szam INT DEFAULT 100000; DECLARE i INT DEFAULT 0; WHILE i

Ad

Answer

As per the docs, you can only use DECLARE at given locations:

DECLARE is permitted only inside a BEGIN ... END compound statement and must be at its start, before any other statements.

Declarations must follow a certain order. Cursor declarations must appear before handler declarations. Variable and condition declarations must appear before cursor or handler declarations.

(You're also missing a space at numberINT.)

Ad
source: stackoverflow.com
Ad