Auto insert values into a MySQL table in a range?



For this, you can create a stored procedure. Let us first create a table.

mysql> create table DemoTable    -> (    -> Value int    -> ); Query OK, 0 rows affected (0.55 sec)

Following is the query to create a stored procedure to auto insert values to a table from range 10 to 20 −

mysql> DELIMITER // mysql> CREATE PROCEDURE AutoInsertValuesToTable()    -> BEGIN    ->    DECLARE startingRange INT DEFAULT 10;    ->    WHILE startingRange <= 20 DO    ->       INSERT DemoTable(Value) VALUES (startingRange );    ->       SET startingRange = startingRange + 1;    ->    END WHILE;    -> END    -> // Query OK, 0 rows affected (0.23 sec) mysql> DELIMITER ;

Here is the query to call the stored procedure −

mysql> call AutoInsertValuesToTable(); Query OK, 1 row affected (1.10 sec)

Now you can check the value is inserted into the above table or not −

mysql> select *from DemoTable;

This will produce the following output −

+-------+ | Value | +-------+ | 10 | | 11 | | 12 | | 13 | | 14 | | 15 | | 16 | | 17 | | 18 | | 19 | | 20 | +-------+ 11 rows in set (0.00 sec)

The value inserted into the above table successfully from range 10 to 20.

Updated on: 2019-07-30T22:30:26+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements