{"id":1407,"date":"2018-09-07T14:36:27","date_gmt":"2018-09-07T05:36:27","guid":{"rendered":"https:\/\/www.oqiita.com\/?p=1407"},"modified":"2018-09-07T14:36:27","modified_gmt":"2018-09-07T05:36:27","slug":"%e3%80%90pl-sql%e3%80%91%e3%83%90%e3%83%ab%e3%82%af%e5%87%a6%e7%90%86","status":"publish","type":"post","link":"https:\/\/www.oqiita.com\/?p=1407","title":{"rendered":"\u3010PL\/SQL\u3011\u30d0\u30eb\u30af\u51e6\u7406"},"content":{"rendered":"<p>\u30d0\u30eb\u30af\u51e6\u7406\u3082\u3084\u3063\u3066\u307f\u305f\u3002<\/p>\n<p><!--more--><\/p>\n<p><\/p>\n<h3 id=\"i-0\">SELECT<\/h3>\n<pre class=\"line-numbers\"><code class=\"language-plsql\">DECLARE\n\n  -- \u30b3\u30ec\u30af\u30b7\u30e7\u30f3\u578b\u3092\u5b9a\u7fa9\n  TYPE tra1_type IS TABLE OF TRANSACTION1%rowtype;\n  \n  -- \u30b3\u30ec\u30af\u30b7\u30e7\u30f3\u578b\u306e\u5909\u6570\u3092\u5b9a\u7fa9\n  tra1 tra1_type;\n\nBEGIN\n\n  SELECT * BULK COLLECT INTO tra1 FROM TRANSACTION1;\n\nEND;\n\/<\/code><\/pre>\n<pre class=\"line-numbers\"><code class=\"language-plsql\">DECLARE\n\n  -- \u30b3\u30ec\u30af\u30b7\u30e7\u30f3\u578b\u3092\u5b9a\u7fa9\n  TYPE tra1_type IS TABLE OF TRANSACTION1%rowtype;\n  \n  -- \u30b3\u30ec\u30af\u30b7\u30e7\u30f3\u578b\u306e\u5909\u6570\u3092\u5b9a\u7fa9\n  tra1 tra1_type;\n\n  -- \u30ab\u30fc\u30bd\u30eb\u5b9a\u7fa9\n  CURSOR tra1_rows IS SELECT * FROM TRANSACTION1; \n\nBEGIN\n  OPEN tra1_rows;\n\n  -- \u30d5\u30a7\u30c3\u30c1\u3059\u308b\n  FETCH tra1_rows BULK COLLECT INTO tra1;\n\n  FOR i IN 1 .. tra1.COUNT LOOP\n    DBMS_OUTPUT.PUT_LINE(tra1(i).NAME);\n  END loop;\n\n  CLOSE tra1_rows;\n\nEND;\n\/<\/code><\/pre>\n<p>\uff12\u901a\u308a\u66f8\u304d\u307e\u3057\u305f\u304c\u3001\u7d50\u679c\u306f\u540c\u3058\u2193<\/p>\n<pre>\nPL\/SQL\u30d7\u30ed\u30b7\u30fc\u30b8\u30e3\u304c\u6b63\u5e38\u306b\u5b8c\u4e86\u3057\u307e\u3057\u305f\u3002\n\nAAA\nBBB\nCCC\nDDD\nEEE\nFFF\nGGG\nHHH\n<\/pre>\n<h3 id=\"i-1\">INSERT<\/h3>\n<pre class=\"line-numbers\"><code class=\"language-plsql\">DECLARE\n\n  -- \u30b3\u30ec\u30af\u30b7\u30e7\u30f3\u578b\u3092\u5b9a\u7fa9\n  TYPE tra1_type IS TABLE OF TRANSACTION1%rowtype;\n\n  -- \u30b3\u30ec\u30af\u30b7\u30e7\u30f3\u578b\u306e\u5909\u6570\u3092\u5b9a\u7fa9\n  tra1 tra1_type;\n\nBEGIN\n\n  SELECT * BULK COLLECT INTO tra1 FROM TRANSACTION1;\n\n  -- TRANSACTION2 \u306b\u65b0\u898f\u767b\u9332\n  FORALL i IN 1 .. tra1.COUNT \n    INSERT INTO TRANSACTION2 VALUES(\n      tra1(i).ID, \n      tra1(i).NAME\n    );\n\n  -- TRANSACTION1 \u3068 TRANSACTION2 \u304c\u540c\u3058\u9805\u76ee\u306a\u3089\u4ee5\u4e0b\u3067\u3082\u826f\u3044\n  -- INSERT INTO TRANSACTION2 VALUES tra1;\n\nEND;\n\/<\/code><\/pre>\n<h3 id=\"i-2\">UPDATE<\/h3>\n<pre class=\"line-numbers\"><code class=\"language-plsql\">DECLARE\n\n  -- \u30b3\u30ec\u30af\u30b7\u30e7\u30f3\u578b\u3092\u5b9a\u7fa9\n  TYPE tra1_id_tyep IS TABLE OF TRANSACTION1.ID%TYPE;\n\n  -- \u30b3\u30ec\u30af\u30b7\u30e7\u30f3\u578b\u306e\u5909\u6570\u3092\u5b9a\u7fa9\n  tra1_id tra1_id_tyep;\n\nBEGIN\n  \n  -- \u53d6\u5f97\n  SELECT ID BULK COLLECT INTO tra1_id FROM TRANSACTION1;\n\n  -- \u5168\u4ef6\u30eb\u30fc\u30d7\u3057\u30661.5\u500d\n  FORALL i IN 1 .. tra1_id.COUNT\n    UPDATE TRANSACTION1 SET PRICE = PRICE * 1.5 WHERE ID = tra1_id(i);\n\nEND;\n\/<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u30d0\u30eb\u30af\u51e6\u7406\u3082\u3084\u3063\u3066\u307f\u305f\u3002<\/p>\n","protected":false},"author":1,"featured_media":733,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[177],"tags":[],"class_list":{"0":"post-1407","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-pl-sql","8":"entry"},"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/www.oqiita.com\/wp-content\/uploads\/2018\/05\/sango_db_icatch.png","jetpack_shortlink":"https:\/\/wp.me\/p8QluP-mH","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.oqiita.com\/index.php?rest_route=\/wp\/v2\/posts\/1407","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.oqiita.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.oqiita.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.oqiita.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.oqiita.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1407"}],"version-history":[{"count":2,"href":"https:\/\/www.oqiita.com\/index.php?rest_route=\/wp\/v2\/posts\/1407\/revisions"}],"predecessor-version":[{"id":1409,"href":"https:\/\/www.oqiita.com\/index.php?rest_route=\/wp\/v2\/posts\/1407\/revisions\/1409"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.oqiita.com\/index.php?rest_route=\/wp\/v2\/media\/733"}],"wp:attachment":[{"href":"https:\/\/www.oqiita.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1407"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.oqiita.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1407"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.oqiita.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1407"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}