0

I'm trying to rename my database tables using the answer given here:

How To have Dynamic SQL in MySQL Stored Procedure

but I'm getting an sql error when I attempt to run the following in phpMyAdmin SQL tab:

delimiter // 
CREATE PROCEDURE dynamic(IN tbl CHAR(64), IN col CHAR(64))
BEGIN
    SET @s = CONCAT('SELECT 'RENAME TABLE ', 
             GROUP_CONCAT('', TABLE_SCHEMA, ''.'', TABLE_NAME,
             ' TO ', TABLE_SCHEMA, ''='.osx_''', TABLE_NAME, '')) AS q
             FROM 
             information_schema.Tables WHERE TABLE_SCHEMA='my_database'';;'
    PREPARE stmt FROM @s;
    EXECUTE stmt;
    DEALLOCATE PREPARE stmt;
END
//
delimiter ;

The error is as follows:

#1064 - 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  
'RENAME TABLE ', GROUP_CONCAT('', TABLE_SCHEMA, ''.'', TABLE_NAME,' at line 3

I'm not seeing the error, can anyone spot it please?

--EDIT--

After reviewing Max's answer I reformatted the query using backticks:

CREATE PROCEDURE dynamic(IN tbl CHAR(64), IN col CHAR(64))
BEGIN
    SET @s = SELECT
             CONCAT('RENAME TABLE ', 
             GROUP_CONCAT('`', TABLE_SCHEMA, '`.`', TABLE_NAME, 
             '` TO `', TABLE_SCHEMA, '`.`osx_', TABLE_NAME, '`')) AS q
             FROM 
             `information_schema`.`Tables` WHERE TABLE_SCHEMA=`my_database`;
    PREPARE stmt FROM @s;
    EXECUTE stmt;
    DEALLOCATE PREPARE stmt;
END

from another example I found (see below), and I'm still getting sql errors:

#1064 - 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 'SELECT
             CONCAT('RENAME TABLE ', 
             GROUP_CONCAT('`', TABLE_SCHEMA, '`.`',' at line 3 

I also tried the other example:

SELECT 
    CONCAT('RENAME TABLE ', 
    GROUP_CONCAT('`', TABLE_SCHEMA, '`.`', TABLE_NAME, 
    '` TO `', TABLE_SCHEMA, '`.`osx_', TABLE_NAME, '`')) AS q
FROM 
    `information_schema`.`Tables` WHERE TABLE_SCHEMA='my_database';

This executes but produces the following warning:

This table does not contain a unique column. 
Grid edit, checkbox, Edit, Copy and Delete features are not available.

and doesn't do anything to the db.

HELP!

Community
  • 1
  • 1
secondman
  • 3,233
  • 6
  • 43
  • 66

1 Answers1

0
'SELECT 'RENAME

You have opened parameter for CONCAT() with a single quote, and when MySQL sees another one it expects next parameter.
You can either escape quotes or use double quotes for string literals.

CONCAT('SELECT "RENAME TABLE ", GROUP_CONCAT( ...
Max Tsepkov
  • 466
  • 6
  • 15