這篇文章主要介紹了MySQL存儲過程中游標怎樣遍歷,具有一定借鑒價值,需要的朋友可以參考下。希望大家閱讀完這篇文章后大有收獲。下面讓小編帶著大家一起了解一下。
成都創(chuàng)新互聯(lián)專注于企業(yè)成都營銷網(wǎng)站建設、網(wǎng)站重做改版、石獅網(wǎng)站定制設計、自適應品牌網(wǎng)站建設、H5場景定制、成都做商城網(wǎng)站、集團公司官網(wǎng)建設、外貿(mào)網(wǎng)站制作、高端網(wǎng)站制作、響應式網(wǎng)頁設計等建站業(yè)務,價格優(yōu)惠性價比高,為石獅等各大城市提供網(wǎng)站開發(fā)制作服務。
mysql存儲過程中游標遍歷的方法:首先取值,取多個字段;然后遍歷數(shù)據(jù)結(jié)束標志,將結(jié)束標志綁定到游標,代碼為【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); -- 遍歷數(shù)據(jù)結(jié)束標志 DECLARE done INT DEFAULT FALSE; -- 游標 DECLARE examIds CURSOR FOR SELECT EXAM_ID FROM t_exam WHERE EXAM_STATUS = 1 or EXAM_STATUS = 2; -- 將結(jié)束標志綁定到游標 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存儲過程中游標怎樣遍歷內(nèi)容對大家有幫助,同時也希望大家多多支持創(chuàng)新互聯(lián),關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,遇到問題就找創(chuàng)新互聯(lián),詳細的解決方法等著你來學習!