描述
这个函数可以修改数据库中所有表的结构和数据,是同时修改的啊,而且每次执行只能修改一个字段。具体使用看下面的dome。
需要的基础
- 存储过程的定义
- 游标
- 变量的定义
- 逻辑运算
- while , if
- delimiter
代码
drop procedure if exists proc01;
delimiter $$;
create procedure proc01(
in lg varchar(255),
in `database` varchar(255),
in `field` varchar(255),
in `type` varchar(255)
)
begin
declare table_name_copy varchar(255) default '';
declare table_index int(11) default 0;
declare cursor_table cursor for select table_name from information_schema.tables where table_schema = `database`;
declare continue handler for not found set table_index = 1;
open cursor_table;
set @response = '';
fetch cursor_table INTO table_name_copy;
while table_index <> 1 do
if exists(select * from information_schema.columns where table_schema = `database` and table_name = `table_name_copy` and column_name = `field`) then
begin
set @tb = table_name_copy;
set @ddl = concat("alter table `", @tb, "` modify column ", `field` ," ", `type`);
prepare sddl from @ddl;
execute sddl;
deallocate prepare sddl;
set @response = concat(@response, ",", @ddl);
set @sqlStr = concat("update `", @tb , "` set ", `field`,"='", lg, "'");
prepare stmt from @sqlStr;
execute stmt;
deallocate prepare stmt;
end;
end if;
fetch cursor_table INTO table_name_copy;
end while;
close cursor_table;
select @response;
end $$;
call proc01('ZH_CN', 'hczn', 'sys_language', 'varchar(255)');
|