问题描述:
当我们在使用sql进行批量处理的时候,例如为表新增10列,涉及了很多重复操作,对于重复操作,能否利用编程思想予以优化呢?
解决思路:
drop procedure if exists t;
delimiter $$
create procedure t()
begin
declare i int;
declare num varchar(2);
set i =1;
set @sqltext='';
while i<10 do
set num=cast(i as char);
set @sqltext = concat(@sqltext,' add c',num,' char(1) not null, ');
set i=i+1;
end while;
set @sqltext=concat('alter table aaa ',@sqltext,'add c10 char(1) not null;');
end $$
delimiter ;
call t();
select @sqltext;
prepare t from @sqltext;
execute t;
select * from aaa;
在第一节基础上 更新了函数和执行顺序:实操了字符数函数、时间函数、字段截取函数、interval函数等,除最后顺序实操因未导入数据库,导致无法执行