DB2 存儲過程中游標循環的嵌套使用方法。

DB2 存儲過程中有時候會在一個遊標循環內部,還有一個遊標循環。

第二個遊標循環中,也會使用到第一個遊標循環中的值作爲輸入參數,來獲取第二個循環結果。


1 遊標循環結構是這樣的:

FOR txn as curs CURSOR WITH HOLD FOR

    select fields_name from table where condition group by condition order by fields

DO

    FOR conn as
         select fields_name from table where txn.fields_name group by condition order by fields

   DO

         To Do the job in here

        

    END FOR;

END FOR;


在 To Do the job  位置可以 組織返回結果。這樣就可以嵌套循環了。


同樣在To Do the job  位置還可以加入select into語句。

如下:

       select count(chgpntcd), sum(kwhconsumed), sum(duration) into totalKWHours, totalKWHours, totalKWHours from ntwpt.rechtxnhis;

       set Response = totalKWHours || totalKWHours || totalKWHours;


2 同樣的也可以在第一個循環裏面並列加入多個二級For循環:

FOR txn as curs CURSOR WITH HOLD FOR

    select fields_name from table where condition group by condition order by fields

DO

   //第一個並列二級循環

    FOR conn as
         select fields_name from table where txn.fields_name group by condition order by fields

   DO

         To Do the job in here

        

    END FOR;

    //第 二個並列二級循環

    FOR conn as
         select fields_name from table where txn.fields_name group by condition order by fields

   DO

         To Do the job in here

        

    END FOR;


END FOR;


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章