MYSQL存储过程,清除指前缀的定表名的数据
DELIMITER $$ DROP PROCEDURE IF EXISTS `drop_table`$$ CREATE DEFINER=`root`@`%` PROCEDURE `drop_table`(IN p_db_name VARCHAR(80),IN p_table_name_prefix VARCHAR(80))
BEGIN
DECLARE var_sql_temp_table VARCHAR(200) DEFAULT NULL;
DECLARE var_sql_table_table VARCHAR(200) DEFAULT NULL; -- sql 语句
DECLARE var_delet_string VARCHAR(200) DEFAULT NULL; -- 保存获取的表名 -- 遍历数据结束标志
DECLARE done INT DEFAULT FALSE;
DECLARE cur CURSOR FOR SELECT * FROM tmp_table_result;
-- 将结束标志绑定到游标
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
DROP TEMPORARY TABLE IF EXISTS tmp_table_result; -- 预处理临时表用的sql语句
SET @var_sql_temp_table = CONCAT("CREATE TEMPORARY TABLE tmp_table_result SELECT table_name FROM information_schema.TABLES WHERE table_schema= '",p_db_name,"' and table_name like '",p_table_name_prefix,"%'"); -- 预处理要执行的动态SQL
PREPARE stmt FROM @var_sql_temp_table;
-- 执行SQL语句
EXECUTE stmt;
-- 释放掉预处理段
DEALLOCATE PREPARE stmt;
-- select * from tmp_table_result; -- 查询下临时表 OPEN cur; -- 开始循环
read_loop: LOOP -- 提取游标里的数据;
-- FETCH cur INTO var_execution_amount,var_fail_type;
FETCH cur INTO var_sql_table_table ;
-- 声明结束的时候
IF done THEN
LEAVE read_loop;
END IF; SET @var_delet_string = CONCAT('delete from ',var_sql_table_table);
PREPARE stmt FROM @var_delet_string;
-- 执行SQL语句
EXECUTE stmt;
-- 释放掉预处理段
DEALLOCATE PREPARE stmt; END LOOP;
-- 关闭游标
CLOSE cur; END$$ DELIMITER ;