从windows备份数据库到linux,存在表名的大写全变成小写的情况,实在不想一个个去改了,便写了个存储过程来处理,有需要的朋友可以参考一二。
需要修复的库:WORK_BASE
先建一个正确表名的库: RIGHT_BASE
DROP PROCEDURE if EXISTS recoverTableName; DELIMITER // CREATE PROCEDURE recoverTableName() BEGIN DECLARE end_flag INT DEFAULT 0; DECLARE wrong_table_name VARCHAR(30); DECLARE right_table_name VARCHAR(30); DECLARE cursor0 CURSOR FOR SELECT a.TABLE_NAME, b.TABLE_NAME FROM information_schema.tables a, information_schema.tables b WHERE a.TABLE_SCHEMA = 'RIGHT_BASE' and b.TABLE_SCHEMA = 'WORK_BASE' and a.TABLE_NAME != b.TABLE_NAME and LOWER(a.TABLE_NAME)=b.TABLE_NAME; DECLARE CONTINUE HANDLER FOR NOT FOUND SET end_flag = 1; OPEN cursor0; REPEAT FETCH cursor0 INTO right_table_name, wrong_table_name; SET @sql_str = CONCAT('ALTER TABLE WORK_BASE.',wrong_table_name,' RENAME WORK_BASE.',right_table_name,';'); PREPARE stmt FROM @sql_str; EXECUTE stmt; DEALLOCATE PREPARE stmt; INSERT INTO `logdb`.`log`(`id`, `base_name`, `sql_str`, `time`) VALUES (UUID(), 'WORK_BASE', @sql_str, NOW()); UNTIL end_flag END REPEAT; CLOSE cursor0; END // DELIMITER ; CALL recoverTableName(); DROP PROCEDURE recoverTableName; SELECT * from `logdb`.`log` where column_name='WORK_BASE' ;