如何更新名称以特殊字符开头的每个表中的列

时间:2015-08-04 09:56:42

标签: mysql sql stored-procedures

我需要更新名称以: -

开头的每个表中的特殊列
InterruptionTypeEnded

我实际上怎么做?。

UPD:

我试图编写存储过程,但我不熟悉它,所以它不起作用:

`REPORT_<"DATE PATERN">`

我有以下错误: ERROR 1146(42S02):表&#39; test.tablename&#39;不存在。

我在这一步 DELIMITER $$ DROP PROCEDURE IF EXISTS `debug_msg`$$ CREATE PROCEDURE debug_msg(enabled INTEGER, msg VARCHAR(255)) BEGIN IF enabled THEN BEGIN select concat("** ", msg) AS '** DEBUG:'; END; END IF; END $$ DELIMITER $$ DROP PROCEDURE IF EXISTS changeColumnType; CREATE PROCEDURE changeColumnType () BEGIN DECLARE v_finished INTEGER DEFAULT 0; DECLARE tableName varchar(100); DEClARE table_cursor CURSOR FOR SELECT TABLE_NAME FROM information_schema.TABLES WHERE TABLE_NAME LIKE '%REPORT_%'; DECLARE CONTINUE HANDLER FOR NOT FOUND SET v_finished = 1; OPEN table_cursor; get_tableName: LOOP FETCH table_cursor INTO tableName; IF v_finished = 1 THEN LEAVE get_tableName; END IF; call debug_msg(1, tableName); ALTER TABLE tableName MODIFY COLUMN TIME VARCHAR(8); END LOOP get_tableName; CLOSE table_cursor; END$$ DELIMITER ;

上失败了

1 个答案:

答案 0 :(得分:0)

通过添加预备声明

解决了这个问题
SET @table = tableName;

SET @s1 = CONCAT('ALTER TABLE ', @table, '  MODIFY COLUMN TIME VARCHAR(8);');
PREPARE stmt FROM @s1;
  EXECUTE stmt;
  DEALLOCATE PREPARE stmt;