我想搜索mysql数据库中含有his字段的数据表,然后批量清空。请写一个存储过程
时间: 2024-06-06 09:06:56 浏览: 111
sql清空表数据后重新添加数据存储过程的示例.docx
DELIMITER //
CREATE PROCEDURE clear_his_tables()
BEGIN
DECLARE tbl_name VARCHAR(255);
DECLARE done INT DEFAULT FALSE;
DECLARE cur CURSOR FOR SELECT table_name FROM information_schema.tables WHERE table_schema = DATABASE() AND table_name LIKE '%his%';
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
OPEN cur;
read_loop: LOOP
FETCH cur INTO tbl_name;
IF done THEN
LEAVE read_loop;
END IF;
SET @sql = CONCAT('TRUNCATE TABLE ', tbl_name);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END LOOP;
CLOSE cur;
END //
DELIMITER ;
阅读全文