BEGIN
declare tmp int default 0;
declare manageCode int(4) default 0001;
declare done int default -1;
/* 声明游标 */
declare myCursor cursor for select id from products where length(manage_code) = 12 and active = 'Y' and org_id = v_org_id;
/* 当游标到达尾部时,mysql自动设置done=1 */
declare continue handler for not found set done=1;
/* 打开游标 */
open myCursor;
/* 循环开始 */
myLoop: LOOP
/* 移动游标并赋值 */
fetch myCursor into tmp;
if done = 1 then
leave myLoop;
end if;
/* do something */
select max(right(manage_code,4)) into manageCode from products
where left(manage_code,12) = (select manage_code from products where id = tmp)
and active='Y' and length(manage_code) = 16 and org_id = v_org_id;
if manageCode is null then
update products set manage_code = CONCAT(manage_code,'0001') where id = tmp;
else
update products set manage_code = CONCAT(manage_code,LPAD(manageCode+1,4,0)) where id = tmp;
end if;
/* 循环结束 */
end loop myLoop;
/* 关闭游标 */
close myCursor;
declare tmp int default 0;
declare manageCode int(4) default 0001;
declare done int default -1;
/* 声明游标 */
declare myCursor cursor for select id from products where length(manage_code) = 12 and active = 'Y' and org_id = v_org_id;
/* 当游标到达尾部时,mysql自动设置done=1 */
declare continue handler for not found set done=1;
/* 打开游标 */
open myCursor;
/* 循环开始 */
myLoop: LOOP
/* 移动游标并赋值 */
fetch myCursor into tmp;
if done = 1 then
leave myLoop;
end if;
/* do something */
select max(right(manage_code,4)) into manageCode from products
where left(manage_code,12) = (select manage_code from products where id = tmp)
and active='Y' and length(manage_code) = 16 and org_id = v_org_id;
if manageCode is null then
update products set manage_code = CONCAT(manage_code,'0001') where id = tmp;
else
update products set manage_code = CONCAT(manage_code,LPAD(manageCode+1,4,0)) where id = tmp;
end if;
/* 循环结束 */
end loop myLoop;
/* 关闭游标 */
close myCursor;
END
执行
DB::query(Database::UPDATE,"call sp_channel()")->execute('rwcd-fp');