方式一、


   
    
  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;



评论关闭
IT序号网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!