truncate-all-tables(위험).sql 990 B

12345678910111213141516171819202122232425262728293031
  1. DROP PROCEDURE IF EXISTS trunctables;
  2. DELIMITER ;;
  3. CREATE PROCEDURE trunctables(theDb varchar(64))
  4. BEGIN
  5. declare tname varchar(64);
  6. declare table_except text;
  7. declare is_found int;
  8. declare tcursor CURSOR FOR
  9. SELECT table_name FROM information_schema.tables WHERE table_type <> 'VIEW' AND table_schema = theDb order by table_name;
  10. set table_except = 'dbr_agroup';
  11. SET FOREIGN_KEY_CHECKS = 0;
  12. OPEN tcursor;
  13. l1: LOOP
  14. FETCH tcursor INTO tname;
  15. if tname = NULL then leave l1; end if;
  16. set is_found = instr(table_except, tname);
  17. insert into aaaa (aaa, bbb) values(table_except, tname);
  18. if is_found = 0 then
  19. set @sql = CONCAT('truncate `', theDB, '`.`', tname, '`');
  20. PREPARE stmt from @sql;
  21. EXECUTE stmt;
  22. DEALLOCATE PREPARE stmt;
  23. end if;
  24. END LOOP l1;
  25. CLOSE tcursor;
  26. SET FOREIGN_KEY_CHECKS = 1;
  27. END ;;
  28. DELIMITER ;
  29. call trunctables('ssokkk_db');