MySQL bulk Add new column stored procedure in table

Source: Internet
Author: User
Tags mysql functions

General traffic is larger than the Web site, and the request log table is created independently of one table per day. The business needs to add a new column for each table, and for half a day, write a stored procedure like this:

Log table structure type tbl_ads_req_20140801, tbl_ads_req_20140802 ...

DELIMITER//create procedure sp2 () begindeclare stime varchar (+);d eclare etime varchar (+);d eclare sName varchar (128); DECLARE lName varchar ($);d eclare sqlvar varchar (;d eclare rest int;set stime = ' 20140801 '; set etime = ' 20140831 '; set SName = CONCAT (' tbl_ads_req_ ', '); set lName = "; set rest = 1;while Rest > 0 do   set stime = (select Date_format ((SE Lect adddate (stime,1)), '%y%m%d '); Set lName = CONCAT (sname,stime); Set Sqlvar=concat (' ALTER TABLE ', LName, ' Add app_package varchar (64) '); Set @v_s =sqlvar; Prepare stmt from @v_s; EXECUTE stmt; Set rest = DATEDIFF (etime,stime); END while;end;//DELIMITER;


Some MySQL functions are involved in the need to check it yourself Oh!

MySQL bulk Add new column stored procedure in table

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.