我需要创建一个动态过程,以便每当我需要重命名表的一列时,我都会传递所需的参数,并且它会毫无错误地执行.所以IF的验证.
此代码生成的错误如下:
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 ‘IF EXISTS (SELECT * FROM information_schema.columns WHERE
table_name = ‘People” at line 1
DROP PROCEDURE IF EXISTS `change_column_name`;
DELIMITER ;;
CREATE PROCEDURE `change_column_name`(IN tableName VARCHAR(100), IN columnName VARCHAR(100), IN newColumnName VARCHAR(100), IN columnType VARCHAR(20), IN defaultValue VARCHAR(100))
BEGIN
SET @query = CONCAT('IF EXISTS (SELECT * FROM information_schema.columns WHERE table_name = \'', tableName, '\' AND column_name = \'', columnName, '\') THEN
ALTER TABLE \'', tableName, '\' CHANGE COLUMN \'', columnName, '\' \'', newColumnName, '\' ', columnType, ' DEFAULT ', defaultValue, ';
END IF;');
PREPARE stmt1 FROM @query;
EXECUTE stmt1;
DEALLOCATE PREPARE stmt1;
END ;;
DELIMITER ;
CALL `change_column_name`('People', 'Nme', 'Name', 'VARCHAR(50)', 'NULL');