Mysql dynamically creates tables, data-_mysql stored procedures

Source: Internet
Author: User
Tags prepare stmt
Copy Code code as follows:

BEGIN
DECLARE ' @i ' int (11);
DECLARE ' @siteCount ' int (11);
DECLARE ' @sqlstr ' VARCHAR (2560);
DECLARE ' @sqlinsert ' VARCHAR (2560); The above declaration variable
SELECT COUNT (0) into the ' @siteCount ' from Tbl_base_site; Count total number of records in table Tbl_base_site
Set ' @i ' = 1;
while (' @i '-1) *300< ' @siteCount ' do//while loop execution
SET @sqlstr = CONCAT (' CREATE TABLE tbl_base_syslog ', ' @i ', ' (syslog_id INT (one) auto_increment PRIMARY KEY,
Create_user VARCHAR (32),
Description text,
Create_time datetime,
site_id INT (11),
IP VARCHAR (64),
version_id SMALLINT (2),
Module_identity VARCHAR (64),
Right_name VARCHAR (64)
)');
Prepare stmt from @sqlstr;
Execute stmt;
The above implementation dynamic creation table
SET @sqlinsert = CONCAT (' INSERT into Tbl_base_syslog ', ' @i ', ' (
SYSLOG_ID,
Create_user,
Description
Create_time,
SITE_ID,
Ip
VERSION_ID,
Module_identity,
Right_name)
SELECT syslog_id,
Create_user,
Description
Create_time,
SITE_ID,
Ip
VERSION_ID,
Module_identity,
Right_name
From Tbl_base_syslog
WHERE site_id in (select tbs.site_id from (select site_id from Tbl_base_site limit ', ' @i ' *300, ', ') as TBS)
Order by syslog_id ');
Prepare stmt from @sqlinsert;
Execute stmt;
The above implementation is inserted into a new table dynamically created from a table query record
SET ' @i ' = ' @i ' +1;

End while;
End
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.