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

mysql存储过程修改表的engine

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-07 15:46:57
Original
1061 people have browsed it

因公司需要将多个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 of this Website
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
Latest Issues
MySQL stops process
From 1970-01-01 08:00:00
0
0
0
Error when installing mysql on linux
From 1970-01-01 08:00:00
0
0
0
phpstudy cannot start mysql?
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template