- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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.
Advertisements