将编码更改为utf8

时间:2019-05-01 14:36:07

标签: mysql

我们想在数据库和表级别将数据库更改为utf8。重要的是我不能使用dbname,因为它可以通过迁移工具进行设置,因此我需要从database()函数中检索它。...

DROP PROCEDURE IF EXISTS UpdateToUTF8;

DELIMITER //
CREATE PROCEDURE UpdateToUTF8 ()
BEGIN
  DECLARE Finished BOOL DEFAULT FALSE;
  DECLARE TableName TEXT;


  DECLARE TablesCursor CURSOR FOR
    SELECT c1.TABLE_NAME
    FROM INFORMATION_SCHEMA.TABLES c1
    WHERE c1.TABLE_SCHEMA = DATABASE() AND c1.TABLE_TYPE='BASE TABLE';

  DECLARE CONTINUE HANDLER FOR NOT FOUND SET Finished = TRUE;

  OPEN TablesCursor;

  MainLoop: LOOP
    FETCH TablesCursor INTO TableName;
    IF Finished THEN
      LEAVE MainLoop;
    END IF;

    SET @tableUpdateQuery = CONCAT('ALTER TABLE `', TableName, '` CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci;');
    PREPARE tableUpdate  FROM @tableUpdateQuery;
    EXECUTE tableUpdate;
    DEALLOCATE PREPARE tableupdate;
  END LOOP;

  CLOSE TablesCursor;

  SET @DataBaseName = database();

  ALTER DATABASE @DataBaseName CHARACTER SET utf8 COLLATE utf8_general_ci;
END //
DELIMITER ;

SET foreign_key_checks = 0;
CALL UpdateToUTF8();
SET foreign_key_checks = 1;

引发以下错误:

[2019-05-01 16:22:38] [42000][1064] You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '@DataBaseName CHARACTER SET utf8 COLLATE utf8_general_ci;
[2019-05-01 16:22:38] #SET @schemaUpdateQ' at line 32

我们还尝试了ALTER DATABASE (SELECT database()) CHARACTER SET utf8 COLLATE utf8_general_ci;

0 个答案:

没有答案