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