mysql 循环只能在存储过程。代码记录
1 CREATE DEFINER=`front`@`%` PROCEDURE `a_1`() 2 BEGIN 3 4 -- 声明变量,接收游标循环变量 5 DECLARE _comid INT; 6 -- 遍历数据结束标志 7 DECLARE done INT DEFAULT 1; 8 9 -- 游标 10 DECLARE cur CURSOR FOR select CompanyId From tbcompany ; 11 -- 游标循环结束设置结束标志为null 12 declare CONTINUE HANDLER FOR SQLSTATE '02000' SET done = null; 13 set done = 1; 14 -- 打开游标 15 OPEN cur; 16 -- 开始循环 17 WHILE ( done is not null) DO 18 FETCH cur INTO _comid; 19 if done is not null then 20 -- TODU 业务代码 21 call company_changeAccount(72,_comid); 22 end if; 23 END WHILE; 24 -- 关闭游标 25 CLOSE cur; 26 END