MySql新增列的时候判断是否存在

drop procedure if exists schema_change;
delimiter ‘;;‘;
create procedure schema_change() begin
    if not exists( select * from information_schema.`COLUMNS` where TABLE_NAME=‘QYTB_DYAQ‘ and COLUMN_NAME=‘BDCQYWBSM‘) then
        alter table QYTB_DYAQ add BDCQYWBSM CHAR(1);
    end if;
end;;
delimiter ‘;‘;
-- 调用存储过程
call schema_change(); 
drop procedure if exists schema_change;

我们通过这段代码感觉只要存储过程的内容体就能实现目的,为什么还要创建一个存储过程来处理呢?

因为Mysql不支持直接写入如上内容体的格式。于是只能曲线救国了。

相关推荐