{"id":1546,"date":"2017-09-02T15:22:11","date_gmt":"2017-09-02T07:22:11","guid":{"rendered":"http:\/\/coderbee.net\/?p=1546"},"modified":"2017-09-02T15:22:11","modified_gmt":"2017-09-02T07:22:11","slug":"bulk-binds-bulk-collect-forall-and-record-processing-in-oracle","status":"publish","type":"post","link":"https:\/\/coderbee.net\/index.php\/db\/20170902\/1546","title":{"rendered":"Bulk Binds (BULK COLLECT &#038; FORALL) and Record Processing in Oracle"},"content":{"rendered":"<h2>\u5f15\u8a00<\/h2>\n<p>Oracle \u4f7f\u7528\u4e24\u79cd\u5f15\u64ce\u6765\u5904\u7406 PL\/SQL \u4ee3\u7801\u3002\u6240\u6709\u5b58\u50a8\u8fc7\u7a0b\u7684\u4ee3\u7801\u7531 PL\/SQL \u5f15\u64ce\u5904\u7406\uff0c\u6240\u6709\u7684 SQL \u7531 SQL \u8bed\u53e5\u6267\u884c\u5668\/SQL \u5f15\u64ce\u5904\u7406\u3002<\/p>\n<p>\u5728\u4e24\u4e2a\u5f15\u64ce\u76f4\u95f4\u7684\u4e0a\u4e0b\u6587\u5207\u6362\u4f1a\u5e26\u6765\u5f00\u9500\u3002\u5982\u679c PL\/SQL \u4ee3\u7801\u5728\u4e00\u4e2a\u96c6\u5408\u4e0a\u5faa\u73af\uff0c\u4e3a\u96c6\u5408\u91cc\u7684\u6bcf\u4e2a\u5143\u7d20\u6267\u884c\u540c\u6837\u7684 DML \u64cd\u4f5c\uff0c\u90a3\u4e48\u53ef\u4ee5\u901a\u8fc7\u4e00\u6b21 bulk \u7ed1\u5b9a\u6574\u4e2a\u96c6\u5408\u5230 DML \u8bed\u53e5\u4e0a\u4ee5\u51cf\u5c11\u4e0a\u4e0b\u6587\u5207\u6362\u3002<\/p>\n<h2>BULK COLLECT<\/h2>\n<p>Bulk \u7ed1\u5b9a\u53ef\u4ee5\u5728\u4ece\u67e5\u8be2\u91cc\u52a0\u8f7d\u6570\u636e\u96c6\u65f6\u63d0\u5347\u6027\u80fd\u3002<code>BULK COLLECT INTO<\/code> \u628a\u67e5\u8be2\u7684\u6570\u636e\u7ed3\u6784\u5316\u7ed1\u5b9a\u5230\u96c6\u5408\u4e0a\u3002<\/p>\n<pre><code class=\"sql\">CREATE TABLE bulk_collect_test AS\nSELECT owner,\n       object_name,\n       object_id\nFROM   all_objects;\n\n\n--  \u4f7f\u7528\nDECLARE\n  TYPE t_bulk_collect_test_tab IS TABLE OF bulk_collect_test%ROWTYPE;\n\n  l_tab    t_bulk_collect_test_tab := t_bulk_collect_test_tab();\n  l_start  NUMBER;\nBEGIN\n\n  SELECT *\n  BULK COLLECT INTO l_tab\n  FROM   bulk_collect_test;\n\nEND;\n<\/code><\/pre>\n<p>\u96c6\u5408\u662f\u7ef4\u62a4\u5728\u5185\u5b58\u91cc\u7684\uff0c\u56e0\u6b64\u4ece\u4e00\u4e2a\u5927\u67e5\u8be2\u91cc\u505a bulk collect \u4f1a\u5bf9\u6027\u80fd\u6709\u663e\u8457\u7684\u5f71\u54cd\u3002\u4e8b\u5b9e\u4e0a\uff0c\u4f60\u4e0d\u5e94\u8be5\u4ee5\u8fd9\u6837\u7684\u65b9\u5f0f\u76f4\u63a5\u4f7f\u7528 bulk collect \u3002\u5e94\u5f53\u4f7f\u7528 <code>LIMIT<\/code> \u5b50\u53e5\u9650\u5236\u8fd4\u56de\u7684\u884c\u6570\uff0c\u8fd9\u8ba9\u4f60\u5f97\u5230 bulk \u65b9\u5f0f\u7684\u597d\u5904\uff0c\u53c8\u4e0d\u4f1a\u5360\u7528\u5927\u91cf\u7684\u670d\u52a1\u5668\u5185\u5b58\u3002<\/p>\n<p><!--more--><\/p>\n<pre><code class=\"sql\">SET SERVEROUTPUT ON\nDECLARE\n  TYPE t_bulk_collect_test_tab IS TABLE OF bulk_collect_test%ROWTYPE;\n\n  l_tab t_bulk_collect_test_tab;\n\n  CURSOR c_data IS\n    SELECT *\n    FROM bulk_collect_test;\nBEGIN\n  OPEN c_data;\n  LOOP\n    FETCH c_data\n    BULK COLLECT INTO l_tab LIMIT 10000;\n    EXIT WHEN l_tab.count = 0;\n\n    -- Process contents of collection here.\n    DBMS_OUTPUT.put_line(l_tab.count || ' rows');\n  END LOOP;\n  CLOSE c_data;\nEND;\n<\/code><\/pre>\n<p>\u4ece Oracle 10g \u53ca\u540e\u7eed\uff0cPL\/SQL \u4f18\u5316\u5668\u628a\u6e38\u6807 <code>for loop<\/code> \u8f6c\u6362\u4e3a <code>bulk collect<\/code>\uff0c\u7528\u4e00\u4e2a\u5927\u5c0f\u4e3a 100 \u7684\u6570\u7ec4\u3002\u8fd9\u5e76\u4e0d\u610f\u5473\u7740\u53ef\u4ee5\u5fd8\u8bb0 <code>bulk collect<\/code>\uff0c\u5b83\u7684\u610f\u4e49\u5728\u4e8e\u53ef\u4ee5\u63a7\u5236\u6570\u7ec4\u7684\u5927\u5c0f\u3002\u5982\u679c\u6570\u636e\u884c\u5f88\u5c0f\uff0c\u53ef\u4ee5\u589e\u5927\u6570\u7ec4\u7684\u5927\u5c0f\uff0c\u5982\u679c\u884c\u5f88\u5bbd\uff0c\u53ef\u4ee5\u7528\u5c0f\u7684\u6570\u7ec4\u3002<\/p>\n<h2>FORALL<\/h2>\n<p><code>FORALL<\/code> \u8bed\u6cd5\u5141\u8bb8\u6211\u4eec\u7ed1\u5b9a\u4e00\u4e2a\u96c6\u5408\u7684\u5185\u5bb9\u5230\u5355\u4e2a DML \u8bed\u53e5\uff0c\u5141\u8bb8 DML \u5bf9\u6bcf\u4e00\u884c\u6267\u884c\u800c\u4e0d\u9700\u8981\u4e0a\u4e0b\u6587\u5207\u6362\u3002<\/p>\n<p>Oracle 9i R2 \u5141\u8bb8 update \u8bed\u53e5\u4f7f\u7528 <code>ROW<\/code> \u5173\u952e\u5b57\u6765\u5b9a\u4e49\u8bb0\u5f55\u3002<\/p>\n<pre><code class=\"sql\">FORALL i IN l_tab.first .. l_tab.last\n    INSERT INTO forall_test VALUES l_tab(i);\n\nFORALL i IN l_tab.first .. l_tab.last\n    UPDATE forall_test\n    SET    ROW = l_tab(i)\n    WHERE  id  = l_id_tab(i);\n<\/code><\/pre>\n<h2>SQL%BULK_ROWCOUNT<\/h2>\n<p>\u6e38\u6807\u7684 <code>SQL%BULK_ROWCOUNT<\/code> \u5c5e\u6027\u7ed9\u51fa\u4e86 <code>FORALL<\/code> \u8bed\u53e5\u4e00\u6b21\u8fed\u4ee3\u91cc\u6bcf\u6761\u8bed\u53e5\u5f71\u54cd\u7684\u884c\u6570\u3002\uff08\u4e5f\u662f\u4e2a\u6570\u7ec4\uff0c\u5bf9\u5e94 bulk \u96c6\u5408\u91cc\u7684\u6bcf\u4e00\u884c\uff0c\u7c7b\u4f3c\u4e8e JDBC <code>executeBatch<\/code> \u8fd4\u56de\u7684\u6570\u7ec4\uff09<\/p>\n<pre><code class=\"sql\">CREATE TABLE bulk_rowcount_test AS\nSELECT *\nFROM   all_users;\n\nSET SERVEROUTPUT ON\nDECLARE\n  TYPE t_array_tab IS TABLE OF VARCHAR2(30);\n  l_array t_array_tab := t_array_tab('SCOTT', 'SYS',\n                                     'SYSTEM', 'DBSNMP', 'BANANA'); \nBEGIN\n  -- Perform bulk delete operation.\n  FORALL i IN l_array.first .. l_array.last \n    DELETE FROM bulk_rowcount_test\n    WHERE username = l_array(i);\n\n  -- Report affected rows.\n  FOR i IN l_array.first .. l_array.last LOOP\n    DBMS_OUTPUT.put_line('Element: ' || RPAD(l_array(i), 15, ' ') ||\n      ' Rows affected: ' || SQL%BULK_ROWCOUNT(i));\n  END LOOP;\nEND;\n<\/code><\/pre>\n<h2>SAVE EXCEPTIONS and SQL%BULK_EXCEPTION<\/h2>\n<p>\u5bf9\u4e8e BULK \u96c6\u5408\u7684\u64cd\u4f5c\uff0c\u5982\u679c\u6ca1\u6709\u5f02\u5e38\u5904\u7406\u5668\uff0c\u5f53\u524d\u7684 bulk \u5df2\u5b8c\u6210\u7684\u64cd\u4f5c\u5c06\u56de\u6eda\u3002\u5982\u679c\u6709\u5f02\u5e38\u5904\u7406\u5668\uff0c\u5728\u5f02\u5e38\u4e4b\u524d\u5df2\u5b8c\u6210\u7684\u5de5\u4f5c\u5c06\u4fdd\u7559\uff0c\u4f46\u4e4b\u540e\u7684\u4e0d\u4f1a\u7ee7\u7eed\u5904\u7406\u3002<code>SAVE EXCEPTIONS<\/code> \u5b50\u53e5\u53ef\u4ee5\u6355\u83b7\u5f02\u5e38\uff0c\u5141\u8bb8\u7ee7\u7eed\u8df3\u8fc7\u4ed6\u4eec\uff08\u5f02\u5e38\uff09\u3002\u540e\u9762\u6211\u4eec\u53ef\u4ee5\u68c0\u6d4b\u5f02\u5e38\uff0c\u901a\u8fc7\u6e38\u6807\u7684 <code>SQL%BULK_EXCEPTION<\/code> \u7684\u5c5e\u6027\u3002<\/p>\n<pre><code class=\"sql\">  -- Perform a bulk operation.\n  BEGIN\n    FORALL i IN l_tab.first .. l_tab.last SAVE EXCEPTIONS\n      INSERT INTO exception_test\n      VALUES l_tab(i);\n  EXCEPTION\n    WHEN ex_dml_errors THEN\n      l_error_count := SQL%BULK_EXCEPTIONS.count;\n      DBMS_OUTPUT.put_line('Number of failures: ' || l_error_count);\n      FOR i IN 1 .. l_error_count LOOP\n        DBMS_OUTPUT.put_line('Error: ' || i || \n          ' Array Index: ' || SQL%BULK_EXCEPTIONS(i).error_index ||\n          ' Message: ' || SQLERRM(-SQL%BULK_EXCEPTIONS(i).ERROR_CODE));\n      END LOOP;\n  END;\n<\/code><\/pre>\n<h2>Bulk Binds and Triggers<\/h2>\n<p>\u5bf9\u4e8e bulk update \u548c delete \uff0c timing points \u4fdd\u6301\u4e0d\u53d8\u3002\u96c6\u5408\u91cc\u7684\u6bcf\u4e00\u884c\u89e6\u53d1\u4e00\u4e2a before statement, before row, after row \u548c after statement timing point \u3002<\/p>\n<p>\u5bf9\u4e8e bulk insert\uff0c\u8bed\u53e5\u7ea7\u7684\u89e6\u53d1\u5668\u53ea\u5728\u6574\u4e2a bulk \u64cd\u4f5c\u7684\u5f00\u59cb\u548c\u7ed3\u675f\u65f6\u89e6\u53d1\uff0c\u800c\u4e0d\u662f\u96c6\u5408\u91cc\u7684\u6bcf\u4e00\u884c\u3002<\/p>\n<hr\/>\n<p>\u6b22\u8fce\u5173\u6ce8\u6211\u7684\u5fae\u4fe1\u516c\u4f17\u53f7: <strong>coderbee\u7b14\u8bb0<\/strong>\uff0c\u53ef\u4ee5\u66f4\u53ca\u65f6\u56de\u590d\u4f60\u7684\u8ba8\u8bba\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"258\" height=\"258\" src=\"https:\/\/coderbee.net\/wp-content\/uploads\/2019\/01\/coderbee-note.jpg\" class=\"alignnone size-full wp-image-1707\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f15\u8a00 Oracle \u4f7f\u7528\u4e24\u79cd\u5f15\u64ce\u6765\u5904\u7406 PL\/SQL \u4ee3\u7801\u3002\u6240\u6709\u5b58\u50a8\u8fc7\u7a0b\u7684\u4ee3\u7801\u7531 &hellip; <a href=\"https:\/\/coderbee.net\/index.php\/db\/20170902\/1546\">\u7ee7\u7eed\u9605\u8bfb <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[23],"tags":[285,99],"_links":{"self":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts\/1546"}],"collection":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/comments?post=1546"}],"version-history":[{"count":1,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts\/1546\/revisions"}],"predecessor-version":[{"id":1547,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts\/1546\/revisions\/1547"}],"wp:attachment":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/media?parent=1546"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/categories?post=1546"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/tags?post=1546"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}