2个回答
展开全部
DELIMITER //
CREATE PROCEDURE drop_table( DB_NAME varchar(50), prefix varchar(16))
BEGIN
DECLARE done INT DEFAULT 0; #游标的标志位
DECLARE a varchar(20);
DECLARE b varchar(40);
DECLARE cur1 CURSOR FOR SELECT table_name FROM information_schema.TABLES WHERE table_schema=DB_NAME and table_name like concat(prefix, '%');
DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET done = 1;
OPEN cur1;
REPEAT
FETCH cur1 INTO a;
IF NOT done THEN
set b=concat('drop table ',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;
//
DELIMITER ;
call drop_table('test', 'pre_');
CREATE PROCEDURE drop_table( DB_NAME varchar(50), prefix varchar(16))
BEGIN
DECLARE done INT DEFAULT 0; #游标的标志位
DECLARE a varchar(20);
DECLARE b varchar(40);
DECLARE cur1 CURSOR FOR SELECT table_name FROM information_schema.TABLES WHERE table_schema=DB_NAME and table_name like concat(prefix, '%');
DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET done = 1;
OPEN cur1;
REPEAT
FETCH cur1 INTO a;
IF NOT done THEN
set b=concat('drop table ',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;
//
DELIMITER ;
call drop_table('test', 'pre_');
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询