{"id":95576,"date":"2024-10-20T02:13:16","date_gmt":"2024-10-19T18:13:16","guid":{"rendered":"https:\/\/server.hk\/cnblog\/95576\/"},"modified":"2024-10-20T02:13:16","modified_gmt":"2024-10-19T18:13:16","slug":"oracle%e6%95%b8%e6%93%9a%e5%ba%ab%e7%9a%84bulk-collect%e7%94%a8%e6%b3%95%e4%b9%8b%e6%89%b9%e9%87%8f%e5%a2%9e%e5%88%aa%e6%94%b9","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/95576\/","title":{"rendered":"Oracle\u6578\u64da\u5eab\u7684BULK COLLECT\u7528\u6cd5\u4e4b\u6279\u91cf\u589e\u522a\u6539"},"content":{"rendered":"<h1 id=\"oracle%e6%95%b8%e6%93%9a%e5%ba%ab%e7%9a%84bulk-collect%e7%94%a8%e6%b3%95%e4%b9%8b%e6%89%b9%e9%87%8f%e5%a2%9e%e5%88%aa%e6%94%b9-oeXhLIPKVh\">Oracle\u6578\u64da\u5eab\u7684BULK COLLECT\u7528\u6cd5\u4e4b\u6279\u91cf\u589e\u522a\u6539<\/h1>\n<p>\u5728Oracle\u6578\u64da\u5eab\u4e2d\uff0cBULK COLLECT\u662f\u4e00\u500b\u5f37\u5927\u7684\u529f\u80fd\uff0c\u80fd\u5920\u6709\u6548\u5730\u63d0\u9ad8\u6578\u64da\u8655\u7406\u7684\u6548\u7387\u3002\u7279\u5225\u662f\u5728\u9032\u884c\u6279\u91cf\u589e\u522a\u6539\u64cd\u4f5c\u6642\uff0cBULK COLLECT\u53ef\u4ee5\u986f\u8457\u6e1b\u5c11\u4e0a\u4e0b\u6587\u5207\u63db\u7684\u6b21\u6578\uff0c\u5f9e\u800c\u63d0\u5347\u6027\u80fd\u3002\u672c\u6587\u5c07\u6df1\u5165\u63a2\u8a0eBULK COLLECT\u7684\u7528\u6cd5\uff0c\u4e26\u63d0\u4f9b\u76f8\u95dc\u7684\u793a\u4f8b\u548c\u4ee3\u78bc\u7247\u6bb5\u3002<\/p>\n<h2 id=\"bulk-collect%e7%9a%84%e5%9f%ba%e6%9c%ac%e6%a6%82%e5%bf%b5-oeXhLIPKVh\">BULK COLLECT\u7684\u57fa\u672c\u6982\u5ff5<\/h2>\n<p>BULK COLLECT\u662f\u4e00\u7a2ePL\/SQL\u8a9e\u53e5\uff0c\u7528\u65bc\u4e00\u6b21\u6027\u5f9e\u6578\u64da\u5eab\u4e2d\u6aa2\u7d22\u591a\u884c\u6578\u64da\u3002\u9019\u7a2e\u65b9\u6cd5\u53ef\u4ee5\u5c07\u67e5\u8a62\u7d50\u679c\u5b58\u5132\u5230\u96c6\u5408\uff08\u5982\u6578\u7d44\u6216\u8868\uff09\u4e2d\uff0c\u5f9e\u800c\u6e1b\u5c11\u5c0d\u6578\u64da\u5eab\u7684\u8a2a\u554f\u6b21\u6578\u3002\u9019\u5728\u8655\u7406\u5927\u91cf\u6578\u64da\u6642\u7279\u5225\u6709\u7528\uff0c\u56e0\u70ba\u5b83\u80fd\u5920\u986f\u8457\u63d0\u9ad8\u6027\u80fd\u3002<\/p>\n<h2 id=\"%e6%89%b9%e9%87%8f%e5%a2%9e%e5%88%aa%e6%94%b9%e7%9a%84%e5%af%a6%e7%8f%be-oeXhLIPKVh\">\u6279\u91cf\u589e\u522a\u6539\u7684\u5be6\u73fe<\/h2>\n<p>\u5728\u9032\u884c\u6279\u91cf\u589e\u522a\u6539\u64cd\u4f5c\u6642\uff0cBULK COLLECT\u901a\u5e38\u8207FORALL\u8a9e\u53e5\u7d50\u5408\u4f7f\u7528\u3002FORALL\u8a9e\u53e5\u5141\u8a31\u6211\u5011\u5c0d\u96c6\u5408\u4e2d\u7684\u6bcf\u4e00\u500b\u5143\u7d20\u57f7\u884cDML\u64cd\u4f5c\uff08\u5982INSERT\u3001UPDATE\u6216DELETE\uff09\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528BULK COLLECT\u548cFORALL\u9032\u884c\u6279\u91cf\u589e\u522a\u6539\u7684\u57fa\u672c\u6b65\u9a5f\uff1a<\/p>\n<h3 id=\"1-%e5%ae%9a%e7%be%a9%e9%9b%86%e5%90%88-oeXhLIPKVh\">1. \u5b9a\u7fa9\u96c6\u5408<\/h3>\n<pre><code>DECLARE\n    TYPE emp_rec IS RECORD (\n        emp_id NUMBER,\n        emp_name VARCHAR2(100)\n    );\n    TYPE emp_tab IS TABLE OF emp_rec INDEX BY PLS_INTEGER;\n    employees emp_tab;\nBEGIN\n    -- \u9019\u88e1\u53ef\u4ee5\u586b\u5145\u96c6\u5408\nEND;<\/code><\/pre>\n<h3 id=\"2-%e4%bd%bf%e7%94%a8bulk-collect%e6%aa%a2%e7%b4%a2%e6%95%b8%e6%93%9a-oeXhLIPKVh\">2. \u4f7f\u7528BULK COLLECT\u6aa2\u7d22\u6578\u64da<\/h3>\n<pre><code>SELECT emp_id, emp_name\nBULK COLLECT INTO employees\nFROM employees_table\nWHERE department_id = 10;<\/code><\/pre>\n<h3 id=\"3-%e4%bd%bf%e7%94%a8forall%e9%80%b2%e8%a1%8c%e6%89%b9%e9%87%8f%e6%93%8d%e4%bd%9c-oeXhLIPKVh\">3. \u4f7f\u7528FORALL\u9032\u884c\u6279\u91cf\u64cd\u4f5c<\/h3>\n<pre><code>FORALL i IN employees.FIRST .. employees.LAST\n    INSERT INTO new_employees_table (emp_id, emp_name)\n    VALUES (employees(i).emp_id, employees(i).emp_name);<\/code><\/pre>\n<h2 id=\"%e7%a4%ba%e4%be%8b%ef%bc%9a%e6%89%b9%e9%87%8f%e6%9b%b4%e6%96%b0%e5%92%8c%e5%88%aa%e9%99%a4-oeXhLIPKVh\">\u793a\u4f8b\uff1a\u6279\u91cf\u66f4\u65b0\u548c\u522a\u9664<\/h2>\n<p>\u9664\u4e86\u63d2\u5165\u64cd\u4f5c\uff0cBULK COLLECT\u548cFORALL\u4e5f\u53ef\u4ee5\u7528\u65bc\u66f4\u65b0\u548c\u522a\u9664\u64cd\u4f5c\u3002\u4ee5\u4e0b\u662f\u6279\u91cf\u66f4\u65b0\u548c\u522a\u9664\u7684\u793a\u4f8b\uff1a<\/p>\n<h3 id=\"%e6%89%b9%e9%87%8f%e6%9b%b4%e6%96%b0%e7%a4%ba%e4%be%8b-oeXhLIPKVh\">\u6279\u91cf\u66f4\u65b0\u793a\u4f8b<\/h3>\n<pre><code>FORALL i IN employees.FIRST .. employees.LAST\n    UPDATE employees_table\n    SET emp_name = 'Updated Name'\n    WHERE emp_id = employees(i).emp_id;<\/code><\/pre>\n<h3 id=\"%e6%89%b9%e9%87%8f%e5%88%aa%e9%99%a4%e7%a4%ba%e4%be%8b-oeXhLIPKVh\">\u6279\u91cf\u522a\u9664\u793a\u4f8b<\/h3>\n<pre><code>FORALL i IN employees.FIRST .. employees.LAST\n    DELETE FROM employees_table\n    WHERE emp_id = employees(i).emp_id;<\/code><\/pre>\n<h2 id=\"%e6%80%a7%e8%83%bd%e8%80%83%e9%87%8f-oeXhLIPKVh\">\u6027\u80fd\u8003\u91cf<\/h2>\n<p>\u4f7f\u7528BULK COLLECT\u548cFORALL\u9032\u884c\u6279\u91cf\u64cd\u4f5c\u6642\uff0c\u6027\u80fd\u7684\u63d0\u5347\u662f\u986f\u800c\u6613\u898b\u7684\u3002\u50b3\u7d71\u7684\u9010\u884c\u8655\u7406\u65b9\u5f0f\u6703\u5c0e\u81f4\u591a\u6b21\u4e0a\u4e0b\u6587\u5207\u63db\uff0c\u9019\u6703\u986f\u8457\u5f71\u97ff\u6027\u80fd\u3002\u800cBULK COLLECT\u548cFORALL\u7684\u7d44\u5408\u80fd\u5920\u5c07\u9019\u4e9b\u64cd\u4f5c\u5408\u4f75\uff0c\u5f9e\u800c\u6e1b\u5c11\u4e86\u9019\u4e9b\u5207\u63db\u7684\u6b21\u6578\u3002<\/p>\n<p>\u7136\u800c\uff0c\u5728\u4f7f\u7528BULK COLLECT\u6642\uff0c\u4e5f\u9700\u8981\u6ce8\u610f\u5167\u5b58\u7684\u4f7f\u7528\u3002\u7576\u6aa2\u7d22\u5927\u91cf\u6578\u64da\u6642\uff0c\u53ef\u80fd\u6703\u5c0e\u81f4\u5167\u5b58\u4e0d\u8db3\u7684\u60c5\u6cc1\u3002\u56e0\u6b64\uff0c\u5efa\u8b70\u5728\u4f7f\u7528BULK COLLECT\u6642\uff0c\u6839\u64da\u5177\u9ad4\u60c5\u6cc1\u8a2d\u7f6e\u5408\u9069\u7684\u6578\u64da\u91cf\u3002<\/p>\n<h2 id=\"%e7%b8%bd%e7%b5%90-oeXhLIPKVh\">\u7e3d\u7d50<\/h2>\n<p>BULK COLLECT\u5728Oracle\u6578\u64da\u5eab\u4e2d\u662f\u4e00\u500b\u975e\u5e38\u6709\u7528\u7684\u529f\u80fd\uff0c\u7279\u5225\u662f\u5728\u9032\u884c\u6279\u91cf\u589e\u522a\u6539\u64cd\u4f5c\u6642\u3002\u901a\u904e\u6709\u6548\u5730\u6e1b\u5c11\u4e0a\u4e0b\u6587\u5207\u63db\u7684\u6b21\u6578\uff0cBULK COLLECT\u80fd\u5920\u986f\u8457\u63d0\u9ad8\u6027\u80fd\u3002\u7121\u8ad6\u662f\u63d2\u5165\u3001\u66f4\u65b0\u9084\u662f\u522a\u9664\u64cd\u4f5c\uff0c\u5408\u7406\u4f7f\u7528BULK COLLECT\u548cFORALL\u90fd\u80fd\u5920\u4f7f\u6578\u64da\u8655\u7406\u66f4\u52a0\u9ad8\u6548\u3002<\/p>\n<p>\u5982\u679c\u60a8\u5c0d\u65bc\u6578\u64da\u5eab\u6027\u80fd\u512a\u5316\u6709\u9032\u4e00\u6b65\u7684\u9700\u6c42\uff0c\u8003\u616e\u4f7f\u7528<a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a>\u4f86\u642d\u5efa\u60a8\u7684\u6578\u64da\u5eab\u74b0\u5883\uff0c\u9019\u5c07\u6709\u52a9\u65bc\u63d0\u5347\u60a8\u7684\u61c9\u7528\u6027\u80fd\u548c\u7a69\u5b9a\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u63a2\u7d22Oracle\u6578\u64da\u5eab\u4e2dBULK COLLECT\u7684\u7528\u6cd5\uff0c\u63d0\u5347\u6279\u91cf\u589e\u522a\u6539\u64cd\u4f5c\u7684\u6548\u7387\uff0c\u512a\u5316\u6578\u64da\u8655\u7406\u6027\u80fd\u3002<\/p>\n","protected":false},"author":0,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-95576","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/95576","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=95576"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/95576\/revisions"}],"predecessor-version":[{"id":95577,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/95576\/revisions\/95577"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=95576"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=95576"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=95576"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}