mysql循环结果集_MySQL存储过程-循环遍历查询到的结果集

参考链接:https://www.cnblogs.com/llq1214/p/11202743.html

自己业务写的存储过程

DROP PROCEDURE IF EXISTS update_community_id_1;
CREATE PROCEDURE `update_community_id_1`()
BEGIN

DECLARE s int DEFAULT 0;
DECLARE i int(11); 
DECLARE c VARCHAR(255);

DECLARE temp CURSOR FOR 
		SELECT
			sor.id,
			so.community_id 
		FROM
			sc_order_receipt sor
			LEFT JOIN `ysh_smartserv_charge`.sc_charge_order so ON sor.order_id = so.id 
		WHERE
			sor.order_type = 1;
			
DECLARE CONTINUE HANDLER FOR NOT FOUND SET s = 1;

open temp;

FETCH temp into i,c;

WHILE s <> 1 DO
	UPDATE sc_order_receipt 
	SET community_id = c 
	WHERE
	id = i;
	FETCH temp into i,c;
END WHILE;

CLOSE temp;
END;

CALL update_community_id_1();

你可能感兴趣的:(mysql,常用小问题,mysql,存储过程)