mysql-根据数据库删除数据表

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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值