数据库表名大小写还原

2022/6/14 6:50:07

本文主要是介绍数据库表名大小写还原,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

从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' ;

 



这篇关于数据库表名大小写还原的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程