1. 程式人生 > >mysql儲存過程遊標遍歷之while篇

mysql儲存過程遊標遍歷之while篇

最近業務需要翻閱了很多關於遊標遍歷使用的帖子   總結一下  直接上程式碼吧

 

 

IN `bindid_in` char(36) --輸入引數

BEGIN
/**
淨值=固定資產原值-累計折舊
累計折舊=月折舊額*已計提月份
月折舊額=(固定資產原價-(固定資產原價*3%的殘值率))/預計使用月限
(10000-(10000*3%))/360=26.94    
**/
/**更新淨值,累計折舊,月折舊額,已計提月份**/    
DECLARE myid VARCHAR(100); -- id
DECLARE myybyz INT; -- 原幣原值
DECLARE mykssyrq VARCHAR(36); --
開始使用日期 DECLARE mysyyx INT; -- 使用月限 DECLARE yjtyf INT; -- 已計提月份 DECLARE yzje DECIMAL(16,2); -- 月折舊額 DECLARE ljzj DECIMAL(16,2); -- 累計折舊 DECLARE jz DECIMAL(16,2); -- 累計折舊 DECLARE a INT; DECLARE b INT; DECLARE c INT; DECLARE d INT; DECLARE done INT DEFAULT 0; -- 標記預設為0 DECLARE lists CURSOR FOR SELECT ID,YBYZ,KSSYRQ,SYYX FROM
BO_EU_BPM_SWKZJB; -- 遊標 DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1; -- 沒有下一條資料 修改標記為1 OPEN lists; FETCH NEXT FROM lists INTO myid,myybyz,mykssyrq,mysyyx; WHILE (done<>1) DO BEGIN SELECT year(NOW()) INTO a; SELECT year(mykssyrq) INTO b; SELECT month(NOW()) INTO c; SELECT month(mykssyrq) INTO
d; SELECT (a-b)*12+(c-d)-1 INTO yjtyf ; -- 已計提月份 SELECT round((myybyz-(myybyz*0.03))/mysyyx,2) INTO yzje; -- 月折舊額 IF (yjtyf >= mysyyx) THEN UPDATE BO_EU_BPM_SWKZJB SET JZ=0,LJZJ=myybyz,YZJE=yzje,YJTYF=yjtyf WHERE ID = myid; -- 更新資料庫 ELSE SELECT yzje*yjtyf INTO ljzj; -- 累計折舊 SELECT myybyz-ljzj INTO jz; -- 淨值 UPDATE BO_EU_BPM_SWKZJB SET JZ=jz,LJZJ=ljzj,YZJE=yzje,YJTYF=yjtyf WHERE ID = myid; -- 更新資料庫 END IF; END; FETCH NEXT FROM lists INTO myid,myybyz,mykssyrq,mysyyx; END WHILE; CLOSE lists; END IN `bindid_in` char(36) --輸入引數 BEGIN /**根據LBBM 查詢表中最大的 ZCBM 找到後加+1 賦值進去 如果沒有 則 LBBM+0001 在賦值進去 根據類別查編碼找財務庫最大的編碼+1 更新到財務庫和實物庫**/ DECLARE cwid VARCHAR(36); -- id DECLARE swid VARCHAR(36); -- id DECLARE mylbbm VARCHAR(36); -- 類別編碼 DECLARE myzcbm VARCHAR(36); -- 資產編碼 DECLARE nmyzcbm VARCHAR(36);-- 資產編碼+1 DECLARE done INT DEFAULT 0; -- 標記預設為0 DECLARE lists CURSOR FOR select ID,LBBM from BO_EU_BPM_CWKPZJB WHERE YY_ID= bindid_in; -- 遊標(財務表) DECLARE lists1 CURSOR FOR select ID from BO_EU_BPM_SWKZJB WHERE YY_ID= bindid_in; -- 遊標(實物表) DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1; -- 沒有下一條資料 修改標記為1 OPEN lists; OPEN lists1; FETCH NEXT FROM lists INTO cwid,mylbbm; FETCH NEXT FROM lists1 INTO swid; WHILE (done<>1) DO BEGIN SELECT MAX(ZCBM) INTO myzcbm FROM BO_EU_BPM_CWKPZJB WHERE LBBM = mylbbm; IF myzcbm ='' THEN SELECT CONCAT(mylbbm,'0000') INTO myzcbm; SELECT (CAST(myzcbm AS SIGNED)+1)+'' INTO nmyzcbm; UPDATE BO_EU_BPM_CWKPZJB set ZCBM = nmyzcbm,SWZCBM = CONCAT(nmyzcbm,'001') WHERE ID = cwid; UPDATE BO_EU_BPM_SWKZJB set ZCBM = nmyzcbm,SWZCBM = CONCAT(nmyzcbm,'001') WHERE ID = swid; ELSE SELECT (CAST(myzcbm AS SIGNED)+1)+'' INTO nmyzcbm; UPDATE BO_EU_BPM_CWKPZJB set ZCBM = nmyzcbm,SWZCBM = CONCAT(nmyzcbm,'001') WHERE ID = cwid; UPDATE BO_EU_BPM_SWKZJB set ZCBM = nmyzcbm,SWZCBM = CONCAT(nmyzcbm,'001') WHERE ID = swid; END IF; END; FETCH NEXT FROM lists INTO cwid,mylbbm; FETCH NEXT FROM lists1 INTO swid; END WHILE; CLOSE lists; CLOSE lists1; END /*雙遊標while迴圈遍歷if巢狀真實案例*/
此文僅供參考學習,順便留個痕跡,方便查閱,如有不足,還請多多指教.