把SQL的执行结果赋值给变量,然后将其作为while的条件,循环执行SQL

主要的SQL脚本如下:

BEGIN
	DECLARE
		V INT DEFAULT 1;

DECLARE
	ARGS INT DEFAULT 0; #声明变量

SELECT
	COUNT(*) INTO ARGS  #给变量赋值
FROM
	(
		SELECT
			A.INVE_AMOUNT AS FIRST_INVEST_MONEY
		FROM
			(
				SELECT
					MIN(INVESTTIME) AS MIN_INVE,
					INVESTAMOUNT AS INVE_AMOUNT,
					INVESTOR AS INVESTOR
				FROM
					DW_ODS.ODS_HZCF_T_INVEST
				WHERE
					FLAG = '30007001002' -- AND INVESTOR = '0558dad47fe24be09bd4880a77b1f8b3' 
				GROUP BY
					INVESTOR
			) AS A
	) AS B;


WHILE V < ARGS 
DO
	#满足条件,执行SQL
	INSERT INTO DW_BDS.TEST
VALUES
	(V, '11');

SET V = V + 1;


END
WHILE;


END


你可能感兴趣的:(把SQL的执行结果赋值给变量,然后将其作为while的条件,循环执行SQL)