时间:2021-07-01 10:21:17 帮助过:3人阅读
#删除创建存储过程
DROP PROCEDURE IF EXISTS FountTable; DELIMITER $$ CREATE PROCEDURE FountTable() BEGIN DECLARE TableName varchar(64);
#声明游标 DECLARE cur_FountTable CURSOR FOR SELECT TABLE_NAME FROM information_schema.TABLES WHERE TABLE_SCHEMA=‘front‘ AND TABLE_NAME LIKE ‘student%‘; DECLARE EXIT HANDLER FOR not found CLOSE cur_FountTable; #打开游标 OPEN cur_FountTable; REPEAT FETCH cur_FountTable INTO TableName; #定义预处理 SET @SQLSTR1 = CONCAT(‘create index Flag on ‘,‘`‘,TableName,‘`‘,‘ (Flag); ‘); SET @SQLSTR2 = CONCAT(‘create index State on ‘,‘`‘,TableName,‘`‘,‘ (State); ‘); SET @SQLSTR3 = CONCAT(‘create index upload on ‘,‘`‘,TableName,‘`‘,‘ (upload); ‘); SET @SQLSTR4 = CONCAT(‘create index ccFlag on ‘,‘`‘,TableName,‘`‘,‘ (lockFlag); ‘); SET @SQLSTR5 = CONCAT(‘create index comes on ‘,‘`‘,TableName,‘`‘,‘ (comes); ‘); ###SET @SQLSTR=CONCAT(@SQLSTR1,@SQLSTR2,@SQLSTR3,@SQLSTR4,@SQLSTR5 ); PREPARE STMT1 FROM @SQLSTR1; PREPARE STMT2 FROM @SQLSTR2; PREPARE STMT3 FROM @SQLSTR3; PREPARE STMT4 FROM @SQLSTR4; PREPARE STMT5 FROM @SQLSTR5; EXECUTE STMT1; EXECUTE STMT2; EXECUTE STMT3; EXECUTE STMT4; EXECUTE STMT5; DEALLOCATE PREPARE STMT1; DEALLOCATE PREPARE STMT2; DEALLOCATE PREPARE STMT3; DEALLOCATE PREPARE STMT4; DEALLOCATE PREPARE STMT5; # SELECT @SQLSTR; UNTIL 0 END REPEAT; #关闭游标 CLOSE cur_FountTable; END $$ DELIMITER ; CALL FountTable();
这里有几个细节:
注意:由于mysql在存储过程当中无法将查询出来的变量名直接作为表名来用,所以这里要用到动态拼接SQL的方法,但是通常的SET CONCAT的方法并不管用,所以这里就使用了PREPARE来进行预编译。
批量处理虽然有时候能提高工作的效率,但是带来的潜在危险也是挺大了,所以在执行之前必须要非常有把握你执行的语句对数据的影响,否则在生成环境就非常危险了
http://www.cnblogs.com/chenmh/p/5308146.html
MySQL 批量Dll操作(转)
标签: