oracle 在儲存過程中寫兩個遊標巢狀迴圈
CREATE OR REPLACE PROCEDURE "PROC_CHZBANKMOTNREC" AS
vid NUMBER(19);--id vzdrpriaccount VARCHAR2(12); vzdrpriaccount2 VARCHAR2(12); vamountNum NUMBER;--出現次數 vrentMonth VARCHAR2(8); vrentMonth2 VARCHAR2(8); vamount number;
---定義第一個遊標-----
CURSOR cur_chz2 IS SELECT ZDRPRIACCOUNT FROM 表名 GROUP BY ZDRPRIACCOUNT HAVING count(ZDRPRIACCOUNT)>1;
---定義第二個遊標-----
CURSOR cur_chz1 IS SELECT BANK.id,BANK.ZDRPRIACCOUNT,BANK.RENTMONTH FROM 表名 bank WHERE BANK.WTSCODE IS NOT NULL AND BANK.ZDRPRIACCOUNT IN (SELECT ZDRPRIACCOUNT FROM 表名 GROUP BY ZDRPRIACCOUNT HAVING count(ZDRPRIACCOUNT)>1) ORDER BY BANK.id DESC;
BEGIN -------開啟遊標cursor------ open cur_chz1;
------開始迴圈------ loop
--------fetch 賦值------------ FETCH cur_chz1 into vid,vzdrpriaccount,vrentMonth; exit when cur_chz1%notfound; ---開啟第二個遊標cursor----------------------- open cur_chz2; loop FETCH cur_chz2 into vzdrpriaccount2; exit when cur_chz2%notfound; dbms_output.put_line('Hello'||totalPaidAmount|| '!'); END loop; --------------- 這裡很重要------------
if cur_chz2%isopen then close cur_chz2; end if;
END loop; if cur_chz1%isopen then close cur_chz1; end if;
END;