MySQL stored procedures enable batch data insertion

Source: Internet
Author: User

I haven't written an article for a long time. I can't find it, but I am too lazy to write it. But today this article must be written, because I know it will definitely be used in the future.

Well, I have always known only the addition, deletion, modification, and query of databases. Other transactions and stored procedures are rarely used. Today, I started to write the project code, and I didn't even have a record in the database, how can I query records that do not exist? I want to add 20 records to a user table. How can I manually add one record ?????
Okay, write a stored procedure! The Code is as follows:

Create procedure alert () specify username varchar (50); declare userpwd varchar (50); declare userturename varchar (50); declare specify varchar (50); declare usermobilephone varchar (50 ); declare userqq varchar (50); declare useraddress varchar (50); declare useremail varchar (50); declare destination int; declare userremark varchar (50); declare userspid int; declare I int; Set userpwd = "123456789"; Set userturename = "testuser"; Set usertelephone = "123456789"; Set usermobilephone = "123456789"; Set userqq = "123456798 "; set useraddress = "24 Xuefu road section"; Set useremail = "asd@qq.com"; Set userisfrezee = 0; Set userparentid = 1; Set userremark = "test_user"; Set userspid = 1; set I = 1; loop1: While (I <21) doset username = Concat ('danger ', I); insert into t_user (user_name, user_pwd, user_true_name, user_telephone, user_mobilephone, login, user_address, user_email, login, login, user_remark, user_sp_id) values (username, userpwd, userturename, usertelephone, usermobilephone, userqq, useraddress, useremail, login, userparentid, userremark, userspid); set I = I + 1; end while loop1; End

After writing the stored procedure, call isertusers;

Question 1: After setting the value of a variable, the declare statement cannot be written after the set statement?

For example:

CREATE PROCEDURE test()BEGINDECLARE a VARCHAR(20);set a = "ddd";DECLARE b VARCHAR(20);END;

The above code returns an error. Change the location:

CREATE PROCEDURE test()BEGINDECLARE a VARCHAR(20);DECLARE b VARCHAR(20);set a = "ddd";END;

Correct! Is there something wrong with me? I feel helpless about this design!

Question 2: MySQL uses "+" to connect two characters ",
Fortunately

concat

How nice the function is, but also solves the type conversion.

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.