ORACLE 建立多個遊標並嵌套迴圈

來源:互聯網
上載者:User

需求:每遍曆遊標2的記錄時分別去遍曆遊標1的資料,並替換後插入暫存資料表

CREATE OR REPLACE PROCEDURE TESTQ

IS

DownDictate VARCHAR2(200) DEFAULT '';

t_IsExist NUMBER:=0;

use_DownDictate VARCHAR2(200) DEFAULT '';

BEGIN

--判斷指令是否會錯誤        

SELECT DOWNDICTATE INTO DownDictate FROM T_MS_AUTOPLANDICTATE WHERE PROCEDURENAME='PKG_MEDI_IMM.QUERY_MEDI_IMM';          

IF DownDictate IS NULL THEN          

RETURN;        

END IF;        

--開始定義遊標

DECLARE   

---定義遊標       1

CURSOR C_PARAMETERS IS           

SELECT FUNCTIONNODECODE,FUNCTIONNODENAME FROM T_MS_FUNCTIONNODES WHERE FUNCTIONID =            (SELECT FUNCTIONID FROM T_MS_FUNCTION WHERE FUNCTIONPROCE='PKG_MEDI_IMM.QUERY_MEDI_IMM');        

---定義遊標          2

CURSOR C_MEDIIMME IS           

SELECT * FROM (  SELECT              A.GRANTMASTER_BARCODE,A.GRANTMASTER_DATE,B.UNITINFO_NAME,C.USERINFO_REALNAME FROM T_LABEL_GRANTMASTER A              LEFT OUTER JOIN T_ER_UNITINFO B ON A.GRANTMASTER_FARMID=B.UNITINFO_ID              LEFT OUTER JOIN T_SYS_USERINFO C ON A.GRANTMASTER_FARMERSID = C.USERINFO_ID              WHERE C.USERINFO_MOBILE IS NOT NULL OR C.USERINFO_PHONE IS NOT NULL) T;

--定義遊標1的行定義

TYPE paraType IS RECORD(           

code     VARCHAR2(20),           

codecn   VARCHAR2(50)         

);         

paraRowType paraType;

---定義遊標2的行定義

 TYPE rtype IS RECORD(            

yzpch    VARCHAR2(50),            

g_date   DATE,            

yzc  VARCHAR2(100),            

yznh   VARCHAR2(50)         

);         

c_rtype rtype;

 

--開始執行         

BEGIN           

OPEN C_MEDIIMME;        --開啟遊標 2              

LOOP            

FETCH C_MEDIIMME INTO c_rtype;            

EXIT WHEN C_MEDIIMME%NOTFOUND;            

use_DownDictate := DownDictate;            

OPEN C_PARAMETERS;       --開啟遊標1      (需要每遍曆遊標2的一條資料時開啟一次遊標1)

--遍曆參數表            

BEGIN              

LOOP                

FETCH C_PARAMETERS INTO paraRowType;                

EXIT WHEN C_PARAMETERS%NOTFOUND;                

IF paraRowType.code = 'yzpch' THEN                   

use_DownDictate := REPLACE(use_DownDictate,paraRowType.codecn||'#',c_rtype.yzpch);                

ELSIF paraRowType.code = 'yznh' THEN                   

use_DownDictate := REPLACE(use_DownDictate,paraRowType.codecn||'#',c_rtype.yznh);                

ELSIF paraRowType.code = 'yzc' THEN                   

use_DownDictate := REPLACE(use_DownDictate,paraRowType.codecn||'#',c_rtype.yzc);                

END IF;              

END LOOP;             

END;             

CLOSE C_PARAMETERS;    --關閉遊標1          

--插入暫存資料表              

INSERT INTO T_MEDIIMMETABLE(ReplaceResult,DateResult)              

VALUES(use_DownDictate,c_rtype.g_date);            

END LOOP;                      

CLOSE C_MEDIIMME;  --關閉遊標2       

END;

END TESTQ;

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.