MySQL uses a while statement to insert data into a data table in bulk

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.