Home >Database >Mysql Tutorial >mysql存储过程修改表的engine

mysql存储过程修改表的engine

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-06-07 15:46:571054browse

因公司需要将多个schema下所有使用MyISAM引用的表更改为InnoDB引擎,所以就定了个存储过程来实现 delimiter // --存储过程中可以使用分号 drop procedure if exists `alter_engine`// -- 若已存在则删除 create procedure `alter_engine`(in schema_name_in

因公司需要将多个schema下所有使用MyISAM引用的表更改为InnoDB引擎,所以就定了个存储过程来实现

 

delimiter // --存储过程中可以使用分号
drop procedure if exists `alter_engine`// -- 若已存在则删除
create procedure `alter_engine`(in schema_name_in varchar(30)) --输入参数:schema名称
begin
 declare alter_sql varchar(100); --用于获取游标查询生成的sql语句
 declare stop_flag int; --游标循环是否结束的标志

 

/*获取指定schema下面使用MyISAM engine的表 , 并直接组装成 alter table table_name engine = InnoDB的SQl语句*/
 declare table_name_cur cursor for select concat('alter table ',table_name,' ENGINE=InnoDB; ')
        from information_schema.tables where table_schema = schema_name_in and engine = 'MyISAM';
/*循环结束标志*/

 declare continue handler for not found set stop_flag = 1;

 set stop_flag = 0;

 open table_name_cur;
 repeat
  fetch table_name_cur into alter_sql;
   if stop_flag = 0 then
     /*prepare from 后面的变量必须是@开头*/

     set @alter_sql = alter_sql;
     prepare s1 from @alter_sql;
      execute s1;

/*显示回收s*/
     deallocate prepare s1;
            end if;
UNTIL stop_flag = 1
END REPEAT;
CLOSE table_name_cur;
END;
//

/*切换回系统默认的命令结束标志*/
delimiter ;

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:在centos 下开启MySQL远程访问Next article:mysql创建表