码迷,mamicode.com
首页 > 数据库 > 详细

mysql 为某一数据库下所有表中添加相同字段

时间:2016-05-16 14:24:47      阅读:1087      评论:0      收藏:0      [点我收藏+]

标签:

BEGIN
  DECLARE s_tablename VARCHAR(100);
 
 /*显示表的数据库中的所有表
 SELECT table_name FROM information_schema.tables WHERE table_schema=‘databasename‘ Order by table_name ;
 */

#显示所有
 DECLARE cur_table_structure CURSOR
 FOR
 SELECT table_name
 FROM INFORMATION_SCHEMA.TABLES
 WHERE table_schema = ‘preschool‘
 ORDER BY table_name;

 DECLARE CONTINUE HANDLER FOR SQLSTATE ‘02000‘ SET s_tablename = NULL;

 OPEN cur_table_structure;

 FETCH cur_table_structure INTO s_tablename;
 WHILE ( s_tablename IS NOT NULL) DO
  SET @MyQuery=CONCAT("alter table `",s_tablename,"` add column sharding_name VARCHAR(30)");
  PREPARE msql FROM @MyQuery;
 
  EXECUTE msql ;#USING @c;
   
  FETCH cur_table_structure INTO s_tablename;
  END WHILE;
 CLOSE cur_table_structure;

    END

mysql 为某一数据库下所有表中添加相同字段

标签:

原文地址:http://www.cnblogs.com/iliuyuet/p/5497841.html

(1)
(1)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!