DROP PROCEDURE IF EXISTS testEndHandle;
DELIMITER $$

 CREATE PROCEDURE testEndHandle()
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 = '数据库名' AND table_name NOT IN (
 SELECT t.table_name  FROM (
     SELECT table_name,column_name FROM information_schema.columns
     WHERE table_name IN (
        SELECT table_name
        FROM INFORMATION_SCHEMA.TABLES
        WHERE table_schema = '数据库名') and table_schema = '数据库名'
     ) t WHERE t.column_name = 'language_code'
 ) AND table_name REGEXP '^表名\_e[0-9]+$';

 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 `字段名` varchar(12) NOT NULL DEFAULT 'en' COMMENT '语言代码' AFTER `site_id`;");
  PREPARE msql FROM @MyQuery;

  EXECUTE msql ;#USING @c;

  FETCH cur_table_structure INTO s_tablename;
  END WHILE;
 CLOSE cur_table_structure;

END;

$$

#执行存储过程
CALL testEndHandle();

Jungle
18 声望2 粉丝

« 上一篇
Mysql数据类型