
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
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.
- Related Articles
- Insert JSON into a MySQL table?
- Split a string and insert it as individual values into a MySQL table?
- MySQL INSERT INTO SELECT into a table with AUTO_INCREMENT
- Insert values in a table by MySQL SELECT from another table in MySQL?
- MySQL query for INSERT INTO using values from another table?
- How to insert data into a table with auto-incremented columns using JDBC?
- How can we insert data into a MySQL table?
- How to insert an array of values in a MySQL table with a single INSERT?
- Insert multiple values in a temporary table with a single MySQL query?
- How can we insert a new row into a MySQL table?
- Select and insert values with preceding zeros in a MySQL table
- Can we insert records in a MySQL table without auto_increment values?
- How can we insert values into a table with the help of MySQL self-computed output?
- How to select particular range of values in a MySQL table?
- INSERT INTO table if a table exists in MySQL else implement CREATE TABLE and create the table

Advertisements