0

I've created a stored procedure to change the column name as below.

Table structure:

CREATE TABLE ``procedurecheck`` ( ``id`` int(10) NOT NULL AUTO_INCREMENT, ``colname`` varchar(50) NOT NULL, ``proccheck1`` varchar(50) NOT NULL, PRIMARY KEY (``id``) ) ENGINE=InnoDB DEFAULT CHARSET=latin1

This is a test code only to check procedures.

DELIMITER $$
CREATE PROCEDURE updateColumn(IN COLNAME varchar (50), IN NEWCOLNAME varchar (50))
BEGIN
  SET @ddl = CONCAT('alter table procedurecheck CHANGE (', COLNAME, ' ', NEWCOLNAME, ' VARCHAR(50) NOT NULL)');
  PREPARE STMT FROM @ddl;
  EXECUTE STMT;
END $$
DELIMITER ;

When i call this function using:

CALL updateColumn('proccheck','newproccheck');

i get an error as follws:

#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 '(proccheck newproccheck VARCHAR(50) NOT NULL)' at line 1

Any pointers would be helpful.

Thanks

4

3 回答 3

1

删除(),例如。

CONCAT('alter table procedurecheck CHANGE ', COLNAME, ' ', NEWCOLNAME, ' VARCHAR(50) NOT NULL');
于 2013-06-24T07:14:38.207 回答
0
如果存在则删除过程 pl_updateColumnName;
分隔符 $$
创建过程 pl_updateColumnName(IN tblName varchar (50), IN COLNAME varchar (50), IN NEWCOLNAME varchar (50))
开始
  SET @ddl = CONCAT('alter table', tblName,'CHANGE', COLNAME, '', NEWCOLNAME, 'VARCHAR(50) NOT NULL');
  从@ddl准备STMT;
  执行STMT;
结束 $$
分隔符;
于 2017-03-07T08:49:19.810 回答
-2

你可以试试这个存储过程:

DROP PROCEDURE IF EXISTS pl_alterColumnName;
DELIMITER $$
CREATE PROCEDURE pl_alterColumnName(IN tblName varchar (50), IN COLNAME varchar (50), IN NEWCOLNAME varchar (50))
BEGIN
  SET @ddl = CONCAT(' alter table ', tblName  ,' CHANGE ', COLNAME, ' ', NEWCOLNAME, ' VARCHAR(50) NOT NULL');
  PREPARE STMT FROM @ddl;
  EXECUTE STMT;
END $$
DELIMITER ;
于 2017-03-07T07:38:36.747 回答