0

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"

3
  • 1
    You need a shell around your code. A procedure, function, trigger, event ... Commented Nov 9, 2016 at 19:52
  • Cant it be just a function that i can just execute? Commented Nov 9, 2016 at 19:54
  • Yes, it can be a function. But it can not be just a few lines of code you execute. You need to call something. Put a function around it. Commented Nov 9, 2016 at 20:01

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.