mysql存储过程修改表的engine

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值