'Loop MySQL to insert data in table
I'm trying to insert data in a database for assign an id to each number
table numbers
id | number
1 | 2560
2 | 2561
And this go on for 100 numbers. I found this for PL/SQL
BEGIN
FOR v_LoopCounter IN 2560..2660 LOOP
INSERT INTO numbers (number)
VALUES (v_LoopCounter);
END LOOP;
END;
Also tried like
BEGIN
FOR v_LoopCounter IN 2560.2660 LOOP;
INSERT INTO numbers (number);
VALUES (v_LoopCounter);
END LOOP;
END;
How can I do this in SQL using phpMyAdmin, for that's what I can use? I tried it but I get this error:
There seems to be an error in your SQL query. The MySQL server error output below, if there is any, may also help you in diagnosing the problem
I also have tried now
SELECT * FROM table_name WHERE
BEGIN
FOR v_LoopCounter IN 2560..2660 LOOP
INSERT INTO numbers (number)
VALUES (v_LoopCounter);
END LOOP;
END;
Solution 1:[1]
Try this SELECT a database first else you will get the no database selected error
DROP PROCEDURE IF EXISTS myFunction;
delimiter $$
CREATE PROCEDURE myFunction()
BEGIN
DECLARE i INT DEFAULT 1;
DECLARE j INT DEFAULT 0;
DROP TABLE IF EXISTS test;
CREATE TEMPORARY TABLE test
(id int, numbers int);
WHILE (i<=100) DO
SET j=i+2560;
INSERT INTO test VALUES(i,j);
SET i=i+1;
END WHILE;
select * from test;
drop table test;
END$$
After that call the procedure
delimiter ;
call myFunction();
Solution 2:[2]
--- You can not use FOR loop in PHPMyAdmin ---
Use this way :-
Print all your queries and copy it.
Paste it in sql tab and press GO.
Done.
Example :-str = ""; for (let i = 8; i < 77; i++) { str = str + "HERE IS YOU QUERY YOU WANT... Like DELETE FROM tabel_name WHERE column_id=+i+;\n" } console.log(str);
Solution 3:[3]
Try :
DELIMITER //
BEGIN
FOR v_LoopCounter IN 2560..2660 LOOP
INSERT INTO numbers (number)
VALUES (v_LoopCounter);
END LOOP;
END
//
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | PC. |
| Solution 2 | Jay Parmar |
| Solution 3 | Juck |
