そんなことが・・・できるのさ。
そう、Stored Procedure ならね。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
DROP PROCEDURE IF EXISTS sp_replace_all; DELIMITER // CREATE PROCEDURE sp_replace_all( IN IN_DBNAME varchar(64), IN IN_REP_FROM varchar(128), IN IN_REP_TO varchar(128)) BEGIN DECLARE W_TABLE_NAME varchar(64); DECLARE W_COLUMN_NAME varchar(64); DECLARE FETCH_STATE INT DEFAULT 0; DECLARE CUR_COLUMN CURSOR FOR SELECT TABLE_NAME, COLUMN_NAME FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = IN_DBNAME AND DATA_TYPE IN ('varchar', 'text', 'tinytext', 'mediumtext', 'longtext'); DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET FETCH_STATE = 1; SET @COLUMNS_COUNT = 0; OPEN CUR_COLUMN; FETCH NEXT FROM CUR_COLUMN INTO W_TABLE_NAME, W_COLUMN_NAME; REPEAT SET @SQL = CONCAT("UPDATE ", W_TABLE_NAME, " SET ", W_COLUMN_NAME, " = REPLACE(", W_COLUMN_NAME, ", '", IN_REP_FROM, "', '", IN_REP_TO, "')"); PREPARE STMT FROM @SQL; EXECUTE STMT; DEALLOCATE PREPARE STMT; SET @COLUMNS_COUNT = @COLUMNS_COUNT + 1; FETCH NEXT FROM CUR_COLUMN INTO W_TABLE_NAME, W_COLUMN_NAME; UNTIL FETCH_STATE END REPEAT; CLOSE CUR_COLUMN; SELECT @COLUMNS_COUNT; END // DELIMITER ; |
こんな感じで実行。
1 |
call sp_replace_all('対象データベース名', '置換前', '置換後'); |
使い終わったら消しておきましょう。
1 |
DROP PROCEDURE sp_replace_all; |