删除所有表的PROCEDURE的方法及代码

删除所有表的PROCEDURE的方法及代码,第1张

删除所有表的PROCEDURE的方法及代码,第2张

让我们来看看下面删除所有表的方法和代码。

CREATE PROCEDURE proc _ drop _ all _ tables()
begin
declare v _ tablename varchar(50);
declare v _ constname varchar(50);
declare flag int default 0;
-定义一个查找所有外键的游标
Declare Cur _ Constraint Cursor for Select Table _ Name,Constraint _ Name
from information _ schema . Table _ constraints
其中Constraint _ type = ' FOREIGN KEY '
and Table _ schema =(Select database());
-定义一个查找所有表的游标
declare _ table cursor for select table _ name from information _ schema . tables
,其中table _ type =' base table '和table _ schema =(select database());
-定义一个用于查找所有视图的游标
declare _ view cursor for select table _ name from information _ schema . tables
,其中table _ type =' view '和table _ schema =(select database());
声明未找到的继续处理程序
set flag = 1;
-循环删除所有外键
open cur _ constraint;
repeat
fetch cur _ constraint到v_tablename,v _ constname
如果flag= 0,则
set @ v _ SQL = concat(' alter table ',v_tablename,' drop foreign key ',v _ constname);
从@v_sql准备stmt
执行stmt
end if;
直到flag = 1
结束重复;
close cur _ constraint;
set flag = 0;
-循环删除所有视图
open cur _ view;
repeat
fetch cur _ view到v _ tablename
如果flag=0,则
set @ v _ SQL = concat(' drop view ',v _ tablename);
从@v_sql准备stmt
执行stmt
end if;
直到flag = 1
结束重复;
set flag = 0;
关闭cur _ view
-循环删除所有表
open cur _ table;
repeat
fetch cur _ table到v _ tablename
如果flag = 0,则
set @ v _ SQL = concat(' drop table ',v _ tablename);
从@v_sql准备stmt
执行stmt
end if;
直到flag = 1
结束重复;
解除分配准备stmt
关闭cur _ table
end;

位律师回复
DABAN RP主题是一个优秀的主题,极致后台体验,无插件,集成会员系统
白度搜_经验知识百科全书 » 删除所有表的PROCEDURE的方法及代码

0条评论

发表评论

提供最优质的资源集合

立即查看 了解详情