1. 程式人生 > >BULK COLLECT全解析

BULK COLLECT全解析

BULK COLLECT BULK COLLECT子句會批量檢索結果,並從SQL引擎傳送到PL/SQL引擎(FORALL是從PL/SQL引擎傳送到SQL引擎)
  1. declare
  2. cursor cur_student isselect student_id,first_name,last_name from student;
  3. begin
  4. for i in cur_student loop
  5. DBMS_OUTPUT.PUT_LINE('student_id is '||i.student_id);
  6. DBMS_OUTPUT.PUT_LINE('first_name is '||i.first_name);
  7. DBMS_OUTPUT.PUT_LINE('last_name is '
    ||i.last_name);
  8. end loop;
  9. end;
  10. 上面這個可以改寫成BULK COLLECT子句。兩者的差別在於,BULK COLLECT子句會立即從STUDENT表獲取全部資料行。因為BULK COLLECT會檢索多行資料,這些資料行儲存在集合變數中。
  11. declare
  12. type student_id_type is table of student.student_id%type;
  13. type first_name_type is table of student.first_name%type;
  14. type last_name_type is table of student.last_name
    %type;
  15. student_id_tab student_id_type;
  16. first_name_tab first_name_type;
  17. last_name_tab last_name_type;
  18. begin
  19. select student_id,first_name,last_name
  20.  BULK COLLECT INTO student_id_tab,first_name_tab,last_name_tab
  21.  FROM student;
  22. for i in student_id_tab.first..student_id_tab.last loop
  23.    DBMS_OUTPUT.PUT_LINE
    ('student_id is '||student_id_tab(i));
  24. DBMS_OUTPUT.PUT_LINE('first_name is '||first_name_tab(i));
  25. DBMS_OUTPUT.PUT_LINE('last_name is '||last_name_tab(i));
  26. end loop;
  27. end;
  28. /
巢狀表1沒用構造器初始化2沒EXTEND擴充套件? 當使用SELECT BULK COLLECT INTO語句田中巢狀表時,他們會自動被初始化,並自動擴充套件。回想一下,通常巢狀表必須在使用它之前進行初始化,呼叫與巢狀表型別同名的構造器函式。在初始化巢狀表之後,如要給它賦予下一個值,必須使用EXTEND方法進行擴充套件。 BULK COLLECT子句類似於遊標迴圈,原因在於當SELECT語句不返回任何記錄時,它不會丟擲NO_DATA_FOUND異常。這樣的話,很有必要檢查返回的集合是否包含資料。 由於BULK COLLECT子句不會限制集合的尺寸,並能自動擴充套件,因此當select語句返回大量資料時,最好限制結果集。通過使用帶有遊標SELECT的BULK COLLECT以及新增LIMIT選項可以實現這個目標。
  1. declare
  2. cursor cur_student isselect student_id,first_name,last_name from student;
  3. type student_id_type is table of student.student_id%type;
  4. type first_name_type is table of student.first_name%type;
  5. type last_name_type is table of student.last_name%type;
  6. student_id_tab student_id_type;
  7. first_name_tab first_name_type;
  8. last_name_tab last_name_type;
  9. v_limit pls_integer :=50;
  10. begin
  11. OPEN cur_student;
  12. loop
  13. fetch cur_student BULK COLLECT INTO student_id_tab,first_name_tab,last_name_tab
  14. LIMIT v_limit;
  15. EXIT WHEN student_id_tab.count=0;
  16. for i in student_id_tab.first..student_id_tab.last loop
  17.    DBMS_OUTPUT.PUT_LINE('student_id is '||student_id_tab(i));
  18. DBMS_OUTPUT.PUT_LINE('first_name is '||first_name_tab(i));
  19. DBMS_OUTPUT.PUT_LINE('last_name is '||last_name_tab(i));
  20. end loop;
  21. end loop;
  22. close cur_student;
  23. end;
  24. /
該指令碼使用帶有LIMIT選項的BULK COLLECT子句,一次性從STUDENT表檢索50行資料。也就是說每個集合最多包含50條記錄。為完成這個目標,在遊標迴圈中使用BULK COLLECT子句。注意,在這種情況下,該迴圈的推出條件基於集合中記錄的數量,而不依賴於cur_student%NOTFOUND屬性 用record
  1. declare
  2. cursor cur_student isselect student_id,first_name,last_name from student;
  3. type rec_student is record(
  4. student_id student.student_id%type,
  5. first_name student.first_name%type,
  6. last_name student.last_name%type);
  7. type student_type is table of rec_student;
  8. student_tab student_type;
  9. v_limit pls_integer :=5;
  10. begin
  11. open cur_student;
  12. loop
  13. fetch cur_student BULK COLLECT INTO student_tab LIMIT v_limit;
  14. DBMS_OUTPUT.PUT_LINE(student_tab.count);
  15. EXIT WHEN student_tab.count=0;
  16. DBMS_OUTPUT.PUT_LINE(student_tab.count);
  17. for i in student_tab.first..student_tab.last loop
  18. DBMS_OUTPUT.PUT_LINE('student_id is '||student_tab(i).student_id);
  19. DBMS_OUTPUT.PUT_LINE('first_name is '||student_tab(i).first_name);
  20. DBMS_OUTPUT.PUT_LINE('last_name is '||student_tab(i).last_name);
  21. end loop;
  22. end loop;
  23. close cur_student;
  24. end;
  25. /
這個limit是限制bulk collect一次取多少行into進去,好比說一共select9行,limit是5 那麼第一次取5行,第二次4行,第三次0.如果沒有EXIT WHEN判斷會報錯 PL/SQL: numeric or value error last_name is Ocampo55student_id is 282first_name is Jonathanlast_name is Jaelestudent_id is 283first_name is Benitalast_name is Perkinsstudent_id is 284first_name is Salewalast_name is Lindemanstudent_id is 285first_name is Paullast_name is Sikingerstudent_id is 286first_name is Robinlast_name is Kelly4 第一個output4 第二個outputstudent_id is 288first_name is Rosemarylast_name is Ellmanstudent_id is 289first_name is Shirleylast_name is Murraystudent_id is 290first_name is Brianlast_name is Roblesstudent_id is 291first_name is D.last_name is Dewitt0 第一個output,0行EXIT  第二個output就不會有了 現在你的測試中出現新的錯誤 ORA-06502: PL/SQL: numeric or value erro,這個問題出現最可能的原因是當最後一次fetch cur bulk collect into n limit 50;的時候,找不到任何記錄,所以n.first和n.last是空的,加一句控制就可以。   通常BULK COLLECT子句也會與INSERT、UPDATE和DELETE一起使用。在下列情況下,BULK COLLECT子句與RETURNING子句一起使用
  1. declare
  2. type row_num_type is table of test.row_num%type index by binary_integer;
  3. type row_text_type is table of test.row_text%type index by binary_integer;
  4. row_num_tab row_num_type;
  5. row_text_tab row_text_type;
  6. begin
  7. deletefrom test
  8. returning row_num,row_text
  9. BULK COLLECT INTO row_num_tab,row_text_tab;
  10. DBMS_OUTPUT.PUT_LINE(sql%rowcount||' rows deleted');
  11. for i in row_text_tab.first..row_text_tab.last loop
  12. DBMS_OUTPUT.PUT_LINE('row_num = '||row_num_tab(i)||' | row_text = '||row_text_tab(i));
  13. end loop;
  14. end;
  15. /
  16. 10 rows deleted
  17. row_num =1| row_text = row 1
  18. row_num =2| row_text = row 2
  19. row_num =3| row_text = row

    相關推薦

    BULK COLLECT解析

    BULK COLLECT BULK COLLECT子句會批量檢索結果,並從SQL引擎傳送到PL/SQL引擎。 (FORALL是從PL/SQL引擎傳送到SQL引擎) de

    Visual Studio 2017各版本安裝包離線下載、安裝解析

    pla 離線文件 win10 unit splay and 文件下載 python擴展 erl 轉自 寂靜·櫻花雨 Visual Studio 2017各版本安裝包離線下載、安裝全解析 感謝IT之家網友 寂靜·櫻花雨 的投稿 關於Visual

    jQuery Ajax 解析

    windows 我不 編輯器 som lsp 開始 cif bob cache jQuery Ajax 全解析 本文地址: jQuery Ajax 全解析 本文作者:QLeelulu 轉載請標明出處! jQuery確實是一個挺好的輕量級的JS框架,能幫助我們快速的開發JS

    FORALL和BULK COLLECT

    indices ont exception 異常 return 結果 創建 code 註意事項 參考:http://www.cnblogs.com/hellokitty1/p/4584333.html 用戶在通過PLSQL編寫程序時,PLSQL通常會在操作上進行交互,當用戶

    Android異步載入解析之開篇瞎扯淡

    com des turn pro 能夠 eat launch 卡頓 ring Android異步載入概述 Android異步載入在Android中使用的很廣泛,除了是由於避免在主線程中做網絡操作。更是為了避免在顯示時由於時間太長而造成ANR,添加顯示的流暢性,特別是像Li

    PHP 常量、PHP 變量解析(超局變量、變量的8種數據類型等)

    ret each 回收 操作系統 js xml name static bject 單獨 常量特點 常量一旦被定義就無法更改或撤銷定義。 常量名不需要開頭的$ 與變量不同,常量貫穿整個腳本是自動全局的。 作用域不影響對常量的訪問 常量值只能是字符串或數字 設置 PHP

    資深站長幹貨:小說網站從建立到盈利解析

    網絡資源從2007年做站,剛好十年了。時間過得真快。因為自己是兼職做站,所以一直斷斷續續,也沒有什麽大的成績。做過地方論壇,電影站,股票站,文章站,小說站等,能嘗試的都嘗試了。學了很多東西,也浪費了不少時間,做為一種愛好,就這樣堅持下來了。從剛開始的虛擬主機,到後來的VPS,再到現在的獨立服務器;經歷過網站沒

    Python 直接賦值、淺拷貝和深度拷貝解析

    ima img 引入 對象的引用 print function 引用 輸出結果 ons 直接賦值:其實就是對象的引用(別名)。 淺拷貝(copy):拷貝父對象,不會拷貝對象的內部的子對象。 深拷貝(deepcopy): copy 模塊的 deepcopy 方法

    java事務處理解析

    成功 spa 做了 開發 overflow lan 進行 訪問 ksh 最近學習java事務,看到一位前輩的系列博客不錯,轉載過來作為記錄 轉載地址:http://www.davenkin.me/post/2013-02-16/40048284001 (一)Jav

    Dagger2 使用解析

    ember factor 控制器 oid 好處 人的 field 會有 幫我 Dagger2 使用全解析 Dagger是一個註入工具,何為註入,我們要生產一批機器人,每個機器人都有一個控制器,我們可以在機器人內部 new 出一個控制器: class Robot {

    【嵌入式】Arduino編程基礎到應用解析

    接口 實現 關於 第一次 學習 wid 標誌位 解碼 post Arduino Author: Andrew.Du 基礎 基礎語法: setup() loop() pinMode(引腳,模式) pinMode(13,OUTPUT);

    PL/SQL批處理語句(一)BULK COLLECT

    數據 使用 for循環 差異 code 基於 name 從表 允許 我們知道PL/SQL程序中運行SQL語句是存在開銷的,因為SQL語句是要提交給SQL引擎處理,這種在PL/SQL引擎和SQL引擎之間的控制轉移叫做上下文卻換,每次卻換時,都有額外的開銷。然而,FORALL和

    NGINX源碼安裝配置詳解(./configure),最解析

    unzip roo without rpc服務 所有 googl 版本 並且 大文件 NGINX ./configure詳解 在"./configure"配置中,"--with"表示啟用模塊,也就是說這些模塊在編譯時不會自動構建&qu

    Hadoop數據操作系統YARN解析

    exe 結點 處理 滿足 apache 不同 容器 黑名單 registry   為了能夠對集群中的資源進行統一管理和調度,Hadoop 2.0引入了數據操作系統YARN。YARN的引入,大大提高了集群的資源利用率,並降低了集群管理成本。首先,YARN允

    史上最解析!大數據在十大行業的應用

    作用 方向 風險 追蹤 谷歌地圖 收集 合規 個人 所有 什麽是大數據?這次我們不談概念,不談理論,避虛就實,關註大數據在十大行業的實際應用。從證券行業到醫療領域,越來越多公司意識到大數據的重要性。2015年Gartner調查顯示,超過75%的公司正在投資或計劃在未來兩年內

    DoTwe幸運28平臺搭建下載en解析(入門篇)

    陌生 搭建 今天 入門 tween 幸運 開發人員 方式 開發 DoTween,Itw幸運28平臺搭建下載【征途源碼論壇zhengtuwl.com】聯系方式:QQ:2747044651幸運28平臺搭建下載een,這些名字作為一個Unity開發人員聽起來並不陌生,它們在動畫方

    Android: 在native中訪問assets解析

    lock mp4 cpp sets 這樣的 內容 jniexport opencl href 本文總結在Android Native C++開發中訪問APK中的assets資源的方法 在CMake中添加相關NDK LIB的 依賴 因為我們接下來用到的一些函數實現在NDK庫l

    配置Spring項目上傳的兩種方式(解析

    enc element xml配置 很多 files dir 前言 name 兩種 歡迎查看Java開發之上帝之眼系列教程,如果您正在為Java後端龐大的體系所困擾,如果您正在為各種繁出不窮的技術和各種框架所迷茫,那麽本系列文章將帶您窺探Java龐大的體系。本系列教程希望

    Activity啟動過程解析

    文章出自:https://www.jianshu.com/p/6037f6fda285 前言 一個App是怎麼啟動起來的? App的程式入口到底是哪裡? Launcher到底是什麼神奇的東西? 聽說還有個AMS的東西,它是做什麼的?

    【轉】Java7/8 中的 HashMap 和 ConcurrentHashMap 解析

    原文出處:https://javadoop.com/post/hashmap 今天發一篇”水文”,可能很多讀者都會表示不理解,不過我想把它作為併發序列文章中不可缺少的一塊來介紹。本來以為花不了多少時間的,不過最終還是投入了挺多時間來完成這篇文章的。 網上關於 HashMap 和 Concur