存储过程 删除 数据库所有表

create procedure del_all_table(in db_name char(20))
begin
    declare tb char(100);
    declare i int default 0;
    declare icount int default 0;
    declare done int default 0;
    declare cur cursor for select table_name from information_schema.`TABLES` where table_schema = db_name and table_type = "BASE TABLE";
    declare continue handler for not found set done = 1;
    select count(*) into icount from information_schema.`TABLES` where table_schema = db_name and table_type = "BASE TABLE";
    set FOREIGN_KEY_CHECKS=0;
    open cur;
    while i<icount do
        fetch cur into tb;
        set @sql := concat("drop table `", tb, "`;");
        prepare stmt from @sql;
        execute stmt;
        deallocate prepare stmt;        
        set i = i + 1;
    end while;
    close cur;    
    set FOREIGN_KEY_CHECKS=1;
end;

本文作者:vanxkr

本文链接:http://www.vanxkr.com/2018/7/Procedure-delete-all-tables

版权声明:本博客所有文章除特别声明外,均采用CC BY-NC-SA 3.0许可协议。转载请注明出处!

Mysql长文本
0 条评论
已登录,注销 取消