I have multiple spaces in the mysql table columns. I am trying to write a procedure to actually replace them with underscores. But I get following error:
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
Here is my procedure:
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 ;
Looks to me its complaining about DECLARE
Prior to writing this question I search in the SO and found following questions:
"Renaming multiple columns in one statement with PostgreSQL"