MySQL bulk copy table data to several other tables

Source: Internet
Author: User

Delimiter//CREATE PROCEDURE OneKey (in newnames varchar (+), in oldname varchar (250), in ID int,in count INT) BEGIN declare numint;---Define variables to loop using Set num= 0;  whileNum <Count DoSET @sqlStmt= CONCAT (' INSERT INTO ',--to dynamically modify the table name requires the concat () method Substring_index (Substring_index (Newnames,', ', (0-count+num)), ', ', 1),--loop intercept string table name' SELECT * from ', oldname, ' where id = ', id);            --source data table name PREPARE stmt from @sqlStmt;        EXECUTE stmt; Set num=num+1; End while; END;//delimiter;

PS: Pay special attention to the concat () method in the concatenation of SQL statements, comma stitching place to leave a space! Otherwise it will go wrong!!!

Newnames: A string of table names to copy data into (example: "' table1 ', ' table2 ', ' table3 ', ' table4 ',......")

Oldname: Source data table

Id:where conditions (no need to do)

Count: Number of new tables to copy

"Focus on understanding the concept of batch: the application of cyclic operation database is not a batch operation!" The data to the database at once, the database to cycle operations, this can be counted as a batch operation!! 】

MySQL bulk copy table data to several other tables

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.