delimiter $$
drop PROCEDURE if EXISTS SyncStoreLogo;
CREATE PROCEDURE SyncStoreLogo()
BEGIN
DECLARE t_sql text; --更新中心库的sql语句很长,使用text类型
DECLARE ts VARCHAR(100);
DECLARE SLogo VARCHAR(100);
DECLARE SCode VARCHAR(100);
DECLARE SCount int;
DECLARE l_sql VARCHAR(4000);
--根据saas库名的规则,找到所有相关库,使用游标逐个库生成更新语句
declare done int default false;
DECLARE tcur CURSOR for SELECT table_schema from information_schema.`TABLES` where table_name ='e_store' and table_schema like 'entERPrisedb%';
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done=1;
SELECT '' into t_sql;
open tcur;
read_loop:loop
FETCH tcur into ts;
--因为saas库中的数据存在历史错误,假设商户表中没有数据,就不更新
set l_sql=CONCAT('select count(1) into @SCount from ',ts,'.e_store;');
SET @sql=l_sql;
PREPARE s1 FROM @sql;
EXECUTE s1;
DEALLOCATE PREPARE s1;
if @SCount>0 then
SET l_sql=CONCAT('select Logo,Code into @SLogo,@SCode from ',ts,'.e_store;');--要更新的Logo字段值
SET @sql=l_sql;
PREPARE s1 FROM @sql;
EXECUTE s1;
DEALLOCATE PREPARE s1;
--生成更新语句
if @SLogo is not null then
SELECT CONCAT(t_sql,"update c_store set Logo=\'",@SLogo,"\' where (Logo='' or Logo is null) and Code=",@SCode,';') into t_sql;
end if;
end if;