DROP PROCEDURE IF EXISTS `add_column_if`;

CREATE PROCEDURE `add_column_if`(IN v_table varchar(50), IN v_column varchar(50),IN v_discription varchar(1000))
begin
  declare stmt         varchar(2000);
  declare v_flag     int;
  
  select count(*) into v_flag from information_schema.columns where table_schema = (select DATABASE()) and table_name = v_table and column_name = v_column;
  if(v_flag=0)
  then 
    set @sqlstr = concat('ALTER TABLE `',v_table,'` ADD COLUMN `',v_column,'` ',v_discription);
    prepare stmt from @sqlstr;
    execute stmt;
    end if;
  commit;
end ;

 

调用:

call add_column_if('xxx','xxx','xxx');