mysql存储过程之循环遍历查询结果集

https://blog.csdn.net/fish_study_csdn/article/details/125263253

CREATE DEFINER=`root`@`%` FUNCTION `test`(intParam INT,strParam VARCHAR(50)) RETURNS varchar(4000) CHARSET utf8mb4
BEGIN
DECLARE resultStr VARCHAR(4000) DEFAULT '';
DECLARE sTempChd VARCHAR(4000);
DECLARE s INT DEFAULT 0;
DECLARE si Int ;
	
	DECLARE album_curosr CURSOR FOR SELECT `name` from sys_dept LIMIT 2;
	
	DECLARE CONTINUE HANDLER FOR NOT FOUND SET s = 1;
	
	open album_curosr;
	
	FETCH  album_curosr INTO  sTempChd;
	SET resultStr = CONCAT( ',', sTempChd );
	
	set si = 0;
	
	WHILE s <> 1 DO
		FETCH  album_curosr INTO  sTempChd;

		if  s <> 1 THEN
			SET resultStr = CONCAT(resultStr,',' , sTempChd);
		end if;

-- 		SET resultStr = CONCAT(resultStr,',' , sTempChd);
	
		
	END WHILE;

	CLOSE album_curosr;


-- RETURN CONVERT(si,CHAR);
RETURN resultStr;
END
posted @ 2022-09-12 14:54  寒冷的雨呢  阅读(1168)  评论(0编辑  收藏  举报