利用存储过程清空数据库下的表
一、需求说明:
在每个数据库中,都存在一个yimq_messages等多个表,现在需要利用存储过程清空表这些表的所有数据。需排除系统特定的库表。
二、拆解说明
首先使用系统维护的表信息,获取所有库的名称,再拼接成sql语句,执行truncate清理过程。根据实际情况筛选出需要清理的库。
select DISTINCT(table_schema) from information_schema.tables
where table_schema not in ('information_schema','performance_schema','sys','mysql','migrate','new-times','redpacket','statistic')
三、组装预编译
为避免报错以及字段冲突,使用预编译的方式。最后需要释放预编译。
set @messages = CONCAT("truncate table ","`",report_name,"`",".yimq_messages");
prepare stmt1 FROM @messages;
EXECUTE stmt1;
四、完成sql语句。
CREATE PROCEDURE `clear_allyimq`()
BEGIN
-- 定义变量
DECLARE s int DEFAULT 0;
DECLARE report_name var