How To Insert Numbers From 1 To 1000 To A Table What I Created In A Procedure?
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
Answer
As per the docs, you can only use DECLARE
at given locations:
DECLARE
is permitted only inside aBEGIN
...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
.)
Related Questions
- → I can't do a foreign key, constraint error
- → How to implement DbDongle::convertTimestamps as workaround of invalid timestamps with MySql strict
- → MySQL error "Foreign key constraint is incorrectly formed"
- → Eloquent Multitable query
- → "Laravel 5.1" add user and project with userId
- → Database backup with custom code in laravel 5 and get the data upto 10 rows from per table in database
- → Laravel 5.1 QueryException when trying to delete a project
- → Using Array in '->where()' for Laravel Query Building
- → Chaining "Count of Columns" of a Method to Single Query Builder
- → Laravel Eloquent Joining Strange query
- → convert time using mysql laravel 5
- → How to update a column after an expiration date in MySQL?
- → Foreign key constraint fails on existing key