【數據庫-MySql】清空所有表格的所有數據


方式一、


   
   
  
  
          
  1. drop procedure if exists del_all_tb;
  2. delimiter $$
  3. create procedure del_all_tb(db char( 20))
  4. begin
  5. declare done int default 0;
  6. declare tb char( 100);
  7. declare cur cursor for select table_name from infoRmation_schema.tables where table_schema = db and table_type = "BASE TABLE";
  8. declare continue handler for not found set done = 1;
  9. open cur;
  10. repeat
  11. fetch cur into tb;
  12. set @ sql := concat( "truncate ", tb, ";");
  13. prepare stmt from @ sql;
  14. execute stmt;
  15. deallocate prepare stmt;
  16. until done end repeat;
  17. close cur;
  18. end $$
  19. delimiter ;
  20. call del_all_tb( "atdps");
  21. drop procedure if exists del_all_tb;

方式二、


   
   
  
  
          
  1. #如果存在del_all_tb存儲過程則刪除del_all_tb存儲過程
  2. drop procedure if exists del_all_tb;
  3. #如果存在 tmpTable 臨時表則刪除 del_all_tb 臨時表
  4. DROP TABLE if EXISTS tmpTable;
  5. #創建 del_all_tb存儲過程
  6. create procedure del_all_tb(db char( 20))
  7. begin
  8. #申明變量
  9. DECLARE tntmp VARCHAR( 100);
  10. #創建臨時表
  11. create table tmpTable (tablename VARCHAR( 100),flag int);
  12. #清空臨時表
  13. truncate TABLE tmpTable;
  14. #將需要清空的表插入到臨時表
  15. INSERT INTO tmpTable(tablename , flag ) ( SELECT table_name , 0 as a FROM information_schema.tables
  16. WHERE table_schema = db and table_type= 'BASE TABLE');
  17. #循環獲取所有的表明以及刪除狀態
  18. SELECT tablename into tntmp FROM tmpTable WHERE flag = 0 limit 1;
  19. WHILE tntmp <> '' DO
  20. #拼寫刪除語句
  21. set @sqlText := concat( "truncate ", tntmp, ";");
  22. prepare stmt from @sqlText;
  23. #執行語句
  24. execute stmt;
  25. #釋放刪除語句
  26. deallocate prepare stmt;
  27. #更新表狀態
  28. UPDATE tmpTable SET flag= 1 WHERE tablename = tntmp;
  29. #選擇一下條語句
  30. SELECT tablename into tntmp FROM tmpTable WHERE flag = 0 limit 1;
  31. END WHILE;
  32. end;
  33. call del_all_tb( "atdps");
  34. #如果存在del_all_tb存儲過程則刪除del_all_tb存儲過程
  35. drop procedure if exists del_all_tb;
  36. #如果存在 tmpTable 臨時表則刪除 del_all_tb 臨時表
  37. DROP TABLE if EXISTS tmpTable;



免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM