因公司需要将多个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 ;