CREATE DEFINER=`root`@`localhost` PROCEDURE `DeleteAllDataTableBydbName`(dbName varchar(555)) leave_name: -- 定义跳出存储过程标记 begin -- 声明变量 declare count int default 0;
-- 判断参数是否空值或者为Null if (dbName='' or dbName is null) then -- 离开存储过程前我们可以做一些操作 select '参数值不能为空值或者为Null'; -- 离开存储过程,当条件达成我们可以直接离开存储过程。 leave leave_name;-- end if;
-- 判断数据库是否存在 if(not exists(select * from information_schema.Schemata where Schema_Name=dbName)) then select '数据库不存在!'; leave leave_name; end if;
-- 获取查询某个数据库表的数量 set count= ( select Count(*) from information_schema.tables where Table_Schema=dbName limit 1 );
-- 声明变量 set @i=0;
-- 循环遍历删除数据表 while (@i<count) do -- 声明临时变量(Concat:连接两个字符串;limit:限制返回的记录数) set @tableName= Concat('drop table ','`',(select table_Name from information_schema.tables where Table_Schema=dbName limit 1),'`');
-- 预备一个SQL语句并为其指定一个名称 prepare sqlStr from @tableName; -- 执行预备语句 execute sqlStr; -- 删除预备语句 drop prepare sqlStr; -- 迭代变量 set @i=(@i+1); end while; select '删除完毕!'; end