1. Create a data table
MySQL>createtable test_while ( int Primarykey=0 rows affected (0.28 sec)
View the structure of a data table
Mysql> descTest_while;+-------+---------+------+-----+---------+-------+|Field|Type| Null | Key | Default |Extra|+-------+---------+------+-----+---------+-------+|Id| int( One)|NO|Pri| NULL | |+-------+---------+------+-----+---------+-------+1Rowinch Set(0.01Sec
2. Create a stored procedure at begin: End writes while loop and insert statement
Mysql>delimiter #mysql> Create procedureTest_two () - begin - DeclareIint default 0; - whileI< Ten Do - Insert intoTest_while (ID)Values(i); - SetI=I+ 1; - End while; - End#Query OK,0Rows Affected (0.00Sec
Note: (1) delimiter Chinese meaning delimiter, delimiter, used in MySQL to set the end character of the statement. The default terminator for MySQL is; Set delimiter # after begin: End with a semicolon-terminated code
The block will not execute, then end with a # terminator after end. Use delimiter after creating the stored procedure; Restore default Settings
(2) Declare defines a variable declare int i default 0; Define an integer variable with an initial value of 0
3. Calling a stored procedure
MySQL> delimiter; MySQL>1 Row affected (0.35 sec)
4. See if the code block in the stored procedure was invoked successfully
Mysql> Select * fromTest_while;+----+|Id|+----+| 0 || 1 || 2 || 3 || 4 || 5 || 6 || 7 || 8 || 9 |+----+TenRowsinch Set(0.00Sec
MySQL uses a while statement to insert data into a data table in bulk