Mysql:重命名表中的多个列或替换多个列中的空格

时间:2016-11-09 19:50:32

标签: mysql stored-procedures mysql-error-1064

我在mysql表列中有多个空格。我正在尝试编写一个程序来实际用下划线替换它们。但我得到以下错误:

ERROR 1064 (42000): 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 'DECLARE col_names CURSOR FOR
  SELECT column_name
  FROM INFORMATION_SCHEMA.COLU' at line 1

这是我的程序:

DELIMITER //
DECLARE col_names CURSOR FOR
  SELECT column_name
  FROM INFORMATION_SCHEMA.COLUMNS
  WHERE table_name = 'myTableName'
  ORDER BY ordinal_position;


select FOUND_ROWS() into num_rows;

SET i = 1;
the_loop: LOOP

   IF i > num_rows THEN
        CLOSE col_names;
        LEAVE the_loop;
    END IF;


    FETCH col_names 
    INTO col_name;     

     UPDATE table_name SET column_name=LOWER(REPLACE(column_name, ' ', '_'))

    SET i = i + 1;  
END LOOP the_loop;
DELIMITER ;

在我看来它抱怨DECLARE

在撰写此问题之前,我搜索了SO并发现了以下问题:

"Renaming multiple columns in one statement with PostgreSQL"

0 个答案:

没有答案