Mysql calling procedure failed when dynamically alter table in it

∥☆過路亽.° 提交于 2019-12-02 07:11:24

There a a few alterations you can make to your procedure to make it more streamlined as well as getting round a few problems.

First using a cursor to select the table names rather than using the two selects your using. Secondly to use a prepared statement to allow you to dynamically set the table name...

DELIMITER $$
CREATE DEFINER=`root`@`localhost` PROCEDURE `ALTER_SUMMER_TABLE`()
BEGIN
    DECLARE done INT DEFAULT 0;
    DECLARE tableName VARCHAR(64);
    declare cur cursor for SELECT TABLE_NAME
                             FROM information_schema.COLUMNS
                            WHERE TABLE_SCHEMA = 'summer_cms'
                            AND COLUMN_NAME = 'add_time';
    DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;

    open cur;

    start_loop: loop
        fetch cur into tableName;
        if (done = 1 )THEN
             LEAVE start_loop;
        END IF;
        SET @sql = CONCAT('ALTER TABLE ', tableName,' ADD COLUMN `add_user_id` INT NOT NULL DEFAULT 0 ');
        PREPARE stmt FROM @sql;
        EXECUTE stmt;

    end loop;

    close cur;
END$$
DELIMITER ;

You could do a few tweaks - only fetch table names where the column doesn't already exist for example.

Here's an example of dynamic sql

drop procedure if exists alter_table;
delimiter //

CREATE DEFINER=`root`@`localhost`  procedure alter_table()

begin
declare tablename varchar(20);
set tablename = 'u';

set @sqlstmt =  concat('ALTER TABLE ', tableName, ' ADD COLUMN ', char(96), 'add_user_id', char(96), ' INT NOT NULL DEFAULT 0 COMMENT', char(39), 'add user id', char(39),';');
prepare stmt from @sqlstmt;
execute stmt;
deallocate prepare stmt;

end //

delimiter ;

Note I have used ascii backticks and single quotes.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!