我在过程中使用以下 sql:
READ_LOOP: LOOP
FETCH CUR INTO xStatus;
IF done THEN LEAVE READ_LOOP;
END IF;
set i = i+1;
if i mod 3 = 0 then
SET DescrStatus = CONCAT(DescrStatus, xStatus, ' | ', CHAR(13));
else if i = 1 then
SET DescrStatus = CONCAT(xStatus, ' | ');
else SET DescrStatus = CONCAT(DescrStatus, xStatus, ' | '); end if;
end if;
END LOOP READ_LOOP;
我想知道循环的最后位置。
最佳答案
通过以下查询获取游标的最后一个值(这应该在循环之外)。
SELECT <column_name> INTO @last_value FROM <table_name> WHERE <where_conditions> ORDER BY <order_column> DESC LIMIT 1;
然后,在FETCH后添加IF语句如下:
IF xStatus = @last_value THEN
SELECT 'Last Loop' AS Status;
END IF;
关于mysql - 如何知道循环的最后位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16553463/