原文:
MySQL中将数据库表名修改成大写的存储过程
创建存储过程的代码:
DROP PROCEDURE IF EXISTS uppercaseTablenames;DELIMITER $CREATE PROCEDURE uppercaseTablenames(IN dbname VARCHAR(200))BEGINDECLARE done INT DEFAULT 0;DECLARE oldname VARCHAR(200);DECLARE cur CURSOR FOR SELECT table_name FROM information_schema.TABLES WHERE table_schema = dbname;DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;OPEN cur;REPEATFETCH cur INTO oldname;SET @newname = UPPER(oldname);SET @isNotSame = @newname <> BINARY oldname;IF NOT done && @isNotSame THENSET @SQL = CONCAT('rename table `',oldname,'` to `', LOWER(@newname), '_tmp` ');PREPARE tmpstmt FROM @SQL;EXECUTE tmpstmt;SET @SQL = CONCAT('rename table `',LOWER(@newname),'_tmp` to `',@newname, '`');PREPARE tmpstmt FROM @SQL;EXECUTE tmpstmt;DEALLOCATE PREPARE tmpstmt;END IF;UNTIL done END REPEAT;CLOSE cur;END$DELIMITER ;使用方法,以数据库名作为参数调用存储过程即可: mysql> call uppercaseTablenames('库名');
注意,在 Windows 系统下需要在 MySQL 的 my.ini 配置文件里的 [mysqld] 下加上“lower_case_table_names=2”,否则表名转大写无效。如下:
[mysqld] ... lower_case_table_names=2 [client] ...参考:
posted on 2019-03-22 10:39 阅读( ...) 评论( ...)