您好,登錄后才能下訂單哦!
這篇文章主要介紹了mysql存儲過程中游標怎樣遍歷,具有一定借鑒價值,需要的朋友可以參考下。希望大家閱讀完這篇文章后大有收獲。下面讓小編帶著大家一起了解一下。
mysql存儲過程中游標遍歷的方法:首先取值,取多個字段;然后遍歷數據結束標志,將結束標志綁定到游標,代碼為【DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;】。
mysql存儲過程中游標遍歷的方法:
CREATE DEFINER=`root`@`%` PROCEDURE `updStatus`() BEGIN DECLARE startTime DATETIME; DECLARE endTime DATETIME; DECLARE curTime DATETIME; DECLARE id VARCHAR(36); DECLARE estatus VARCHAR(4); -- 遍歷數據結束標志 DECLARE done INT DEFAULT FALSE; -- 游標 DECLARE examIds CURSOR FOR SELECT EXAM_ID FROM t_exam WHERE EXAM_STATUS = 1 or EXAM_STATUS = 2; -- 將結束標志綁定到游標 DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE; OPEN examIds; -- 遍歷 read_loop: LOOP -- 取值 取多個字段 FETCH NEXT from examIds INTO id; IF done THEN LEAVE read_loop; END IF; SELECT EXAM_STATUS INTO estatus FROM t_exam WHERE EXAM_ID = id ; IF estatus =1 THEN SELECT NOW() INTO curTime; SELECT EXAM_START_TIME INTO startTime from t_exam WHERE EXAM_ID = id ; SELECT EXAM_END_TIME INTO endTime from t_exam WHERE EXAM_ID = id ; IF curTime >= startTime AND endTime > curTime THEN UPDATE t_exam SET EXAM_STATUS = 2 WHERE EXAM_ID = id; ELSEIF curTime >= endTime THEN UPDATE t_exam SET EXAM_STATUS = 3 WHERE EXAM_ID = id; END IF; ELSE SELECT NOW() INTO curTime; SELECT EXAM_END_TIME INTO endTime from t_exam WHERE EXAM_ID = id ; IF curTime >= endTime THEN UPDATE t_exam SET EXAM_STATUS = 3 WHERE EXAM_ID = id; END IF; END IF; END LOOP; CLOSE examIds; END
感謝你能夠認真閱讀完這篇文章,希望小編分享mysql存儲過程中游標怎樣遍歷內容對大家有幫助,同時也希望大家多多支持億速云,關注億速云行業資訊頻道,遇到問題就找億速云,詳細的解決方法等著你來學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。