CREATE PROCEDURE Clear_DB(
DB_NAME varchar(50) # 数据库名称
)
BEGIN
DECLARE done INT DEFAULT 0; #游标的标志位
DECLARE a varchar(20);
DECLARE b varchar(20);
DECLARE cur1 CURSOR FOR SELECT table_name FROM information_schema.TABLES WHERE table_schema=DB_NAME ;
DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET done = 1;
OPEN cur1;
REPEAT
FETCH cur1 INTO a;
IF NOT done THEN
set b=concat('Delete from ',DB_NAME,'.',a); # 拼删除命令
# set b=concat('TRUNCATE from ',DB_NAME,'.',a); # 拼删除命令
SET @E=b;
PREPARE stmt1 FROM @E;
EXECUTE stmt1; # 执行命令
DEALLOCATE PREPARE stmt1; #释放对象
END IF;
UNTIL done END REPEAT;
CLOSE cur1;
END;
|