儲存過程雙遊標
阿新 • • 發佈:2018-12-13
CREATE PROCEDURE 儲存過程名稱() BEGIN DECLARE column 資料型別 DEFAULT 預設值; DECLARE column1 資料型別 DEFAULT 預設值; DECLARE sum INT DEFAULT 0; DECLARE count INT DEFAULT 0; DECLARE done INT DEFAULT 0; DECLARE cur1 CURSOR FOR (SELECT * FROM a ); DECLARE cur2 CURSOR FOR (SELECT * FROM b); DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET done = null; SELECT count(*) FROM b INTO count; OPEN cur1; FETCH cur1 INTO culumn[,column1,column2]; WHILE (done IS NOT NULL) DO IF count > 0 THEN OPEN cur2; #賦值給全域性變數 FETCH cur2 INTO culumn[,column1,column2]; WHILE (sum < count)DO #業務邏輯 #賦值給全域性變數 FETCH cur2 INTO culumn[,column1,column2]; SET sum = sum+1; END WHILE; SET done = 0; SET sum = 0; CLOSE cur2; END IF; FETCH cur1 INTO culumn[,column1,column2]; END WHILE; CLOSE cur1; END