mysql存儲過程批量刪除數(shù)據(jù)庫

測試用的數(shù)據(jù)庫都是以“tb_”開頭的,可以先篩選查詢出要刪除的數(shù)據(jù)庫,再遍歷游標刪除數(shù)據(jù)庫。

delimiter //
drop procedure if exists drop_db;
CREATE PROCEDURE "drop_db"()
BEGIN
  declare dynamicsql varchar(500);
  declare tname varchar(64);
  declare flag int default 0;
  -- 打印要刪除的數(shù)據(jù)庫
  select t.schema_name from information_schema.schemata t where t.schema_name like 'tb_%';
  -- 定義游標
  declare tnames cursor for select t.schema_name from information_schema.schemata t where t.schema_name like 'tb_%';
  declare continue handler for not found set flag=1;
  
  open tnames;
    fetch tnames into name;
    while flag <> 1 do
          -- 拼接動態(tài)sql
      set dynamicsql=concat('DROP DATABASE IF EXISTS ', tname);
      set @executesql=dynamicsql;
      prepare preparesql from @executesql;
      excute preparesql;
      DEALLOCATE prepare preparesql;
      fetch tnames into tname;
    end while;
  close tnames;
 END
 //
 call drop_db();
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容