夢(mèng)幻之旅

          DEBUG - 天道酬勤

             :: 首頁(yè) :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
            671 隨筆 :: 6 文章 :: 256 評(píng)論 :: 0 Trackbacks
          FORALL與BULK COLLECT的使用方法:
          1.使用FORALL比FOR效率高,因?yàn)榍罢咧磺袚Q一次上下文,而后者將是在循環(huán)次數(shù)一樣多個(gè)上下文間切換。
          2.使用BLUK COLLECT一次取出一個(gè)數(shù)據(jù)集合,比用游標(biāo)條取數(shù)據(jù)效率高,尤其是在網(wǎng)絡(luò)不大好的情況下。但BLUK COLLECT需要大量?jī)?nèi)存。

          1).在select into 中使用 forall bulk collect;
          create table test_forall ( user_id number(10), user_name varchar2(20)); 
          SELECT * FROM ex_edm_esp; 
          SELECT * FROM test_forall;
          DECLARE
              TYPE forall_table 
          IS TABLE OF test_forall%ROWTYPE;
              test_table forall_table;
          BEGIN
              
          SELECT ID,esp_name BULK COLLECT INTO test_table FROM ex_edm_esp;
              FORALL idx 
          IN test_table.first..test_table.last
                     
          INSERT INTO test_forall VALUES test_table(idx);
              
          COMMIT;
          EXCEPTION
            
          WHEN OTHERS THEN
              
          ROLLBACK;
          END;

          for all 的另一種寫(xiě)法:
          FORALL idx IN VALUE OF test table

          2).在fetch into中使用 forall, indices of, bulk collect;
          DECLARE
              TYPE forall_table 
          IS TABLE OF test_forall%ROWTYPE;
              test_table forall_table;
              
          CURSOR test_cur IS SELECT ID,esp_name FROM ex_edm_esp;
          BEGIN
              
          OPEN test_cur;
              
          FETCH test_cur BULK COLLECT INTO test_table;
              
          CLOSE test_cur;
              
              
          -- 跳過(guò)集合中的null元素
              FORALL idx IN INDICES OF test_table
                     
          INSERT INTO test_forall VALUES test_table(idx);
              
          COMMIT;
          EXCEPTION
            
          WHEN OTHERS THEN
              
          ROLLBACK;
          END;

          3).在returning into 之中使用 forall in, value of, bulk collect;
          DECLARE
              TYPE t_user_id 
          IS TABLE OF test_forall.user_id%TYPE;
              t1 t_user_id;
              TYPE t_user_name 
          IS TABLE OF test_forall.user_name%TYPE;
              t2 t_user_name;
          BEGIN
              
          DELETE FROM test_forall RETURNING user_iduser_name BULK COLLECT INTO t1,t2;
              
          FOR i IN t1.first..t1.last
                LOOP
                  dbms_output.put_line(t1(i));
                  dbms_output.put_line(t2(i));
                
          END LOOP;
              
          ROLLBACK;
          EXCEPTION
            
          WHEN OTHERS THEN
              
          ROLLBACK;
          END;



          posted on 2011-07-13 11:19 HUIKK 閱讀(1903) 評(píng)論(0)  編輯  收藏 所屬分類: DataBase
          主站蜘蛛池模板: 津南区| 赞皇县| 吉隆县| 翼城县| 彰化县| 涞水县| 满城县| 松原市| 琼海市| 邢台县| 永康市| 横峰县| 阿鲁科尔沁旗| 大名县| 廊坊市| 赤城县| 文安县| 涞水县| 扎鲁特旗| 大竹县| 平舆县| 井冈山市| 张家口市| 东兰县| 通州市| 大方县| 杭锦后旗| 枞阳县| 麻江县| 霍邱县| 田林县| 永济市| 樟树市| 科尔| 通许县| 宜良县| 黄龙县| 深泽县| 桐乡市| 合肥市| 南丰县|