{"id":93622,"date":"2024-10-19T15:32:17","date_gmt":"2024-10-19T07:32:17","guid":{"rendered":"https:\/\/server.hk\/cnblog\/93622\/"},"modified":"2024-10-19T15:32:17","modified_gmt":"2024-10-19T07:32:17","slug":"ora-56901-%e4%b8%8d%e5%85%81%e8%a8%b1%e5%b0%8d-pivotunpivot-%e5%80%bc%e4%bd%bf%e7%94%a8%e9%9d%9e%e5%b8%b8%e9%87%8f%e8%a1%a8%e9%81%94%e5%bc%8f-oracle-%e5%a0%b1%e9%8c%af-%e6%95%85%e9%9a%9c%e4%bf%ae","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/93622\/","title":{"rendered":"ORA-56901: \u4e0d\u5141\u8a31\u5c0d pivot|unpivot \u503c\u4f7f\u7528\u975e\u5e38\u91cf\u8868\u9054\u5f0f ORACLE \u5831\u932f \u6545\u969c\u4fee\u5fa9 \u9060\u7a0b\u8655\u7406"},"content":{"rendered":"<h1 id=\"ora-56901-%e4%b8%8d%e5%85%81%e8%a8%b1%e5%b0%8d-pivotunpivot-%e5%80%bc%e4%bd%bf%e7%94%a8%e9%9d%9e%e5%b8%b8%e9%87%8f%e8%a1%a8%e9%81%94%e5%bc%8f-oracle-%e5%a0%b1%e9%8c%af-%e6%95%85%e9%9a%9c%e4%bf%ae-AFJXQaIvUx\">ORA-56901: \u4e0d\u5141\u8a31\u5c0d pivot|unpivot \u503c\u4f7f\u7528\u975e\u5e38\u91cf\u8868\u9054\u5f0f ORACLE \u5831\u932f \u6545\u969c\u4fee\u5fa9 \u9060\u7a0b\u8655\u7406<\/h1>\n<p>\u5728\u4f7f\u7528 Oracle \u6578\u64da\u5eab\u9032\u884c\u6578\u64da\u8655\u7406\u6642\uff0c\u958b\u767c\u8005\u53ef\u80fd\u6703\u9047\u5230\u5404\u7a2e\u932f\u8aa4\uff0c\u5176\u4e2d\u4e4b\u4e00\u4fbf\u662f <strong>ORA-56901<\/strong> \u932f\u8aa4\u3002\u9019\u500b\u932f\u8aa4\u901a\u5e38\u51fa\u73fe\u5728\u4f7f\u7528 <code>PIVOT<\/code> \u6216 <code>UNPIVOT<\/code> \u64cd\u4f5c\u6642\uff0c\u7576\u5617\u8a66\u4f7f\u7528\u975e\u5e38\u91cf\u8868\u9054\u5f0f\u4f5c\u70ba\u8f49\u63db\u503c\u6642\uff0cOracle \u6703\u5831\u544a\u6b64\u932f\u8aa4\u3002\u672c\u6587\u5c07\u6df1\u5165\u63a2\u8a0e\u6b64\u932f\u8aa4\u7684\u539f\u56e0\u3001\u89e3\u6c7a\u65b9\u6848\u4ee5\u53ca\u5982\u4f55\u9032\u884c\u9060\u7a0b\u8655\u7406\u3002<\/p>\n<h2 id=\"ora-56901-%e9%8c%af%e8%aa%a4%e7%9a%84%e5%8e%9f%e5%9b%a0-AFJXQaIvUx\">ORA-56901 \u932f\u8aa4\u7684\u539f\u56e0<\/h2>\n<p>\u7576\u4f7f\u7528 <code>PIVOT<\/code> \u6216 <code>UNPIVOT<\/code> \u9032\u884c\u6578\u64da\u8f49\u63db\u6642\uff0cOracle \u8981\u6c42\u8f49\u63db\u7684\u503c\u5fc5\u9808\u662f\u5e38\u91cf\u3002\u9019\u610f\u5473\u8457\uff0c\u60a8\u4e0d\u80fd\u4f7f\u7528\u8b8a\u91cf\u6216\u8a08\u7b97\u8868\u9054\u5f0f\u4f86\u4f5c\u70ba\u8f49\u63db\u7684\u4f9d\u64da\u3002\u7576\u60a8\u5617\u8a66\u9019\u6a23\u505a\u6642\uff0cOracle \u6703\u8fd4\u56de <strong>ORA-56901<\/strong> \u932f\u8aa4\uff0c\u63d0\u793a\u4e0d\u5141\u8a31\u4f7f\u7528\u975e\u5e38\u91cf\u8868\u9054\u5f0f\u3002<\/p>\n<h3 id=\"%e7%a4%ba%e4%be%8b-AFJXQaIvUx\">\u793a\u4f8b<\/h3>\n<p>\u4ee5\u4e0b\u662f\u4e00\u500b\u7c21\u55ae\u7684\u793a\u4f8b\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u5f15\u767c\u6b64\u932f\u8aa4\uff1a<\/p>\n<pre><code>SELECT *\nFROM sales\nPIVOT (\n    SUM(amount)\n    FOR product_id IN (SELECT product_id FROM products)  -- \u9019\u88e1\u4f7f\u7528\u4e86\u975e\u5e38\u91cf\u8868\u9054\u5f0f\n);\n<\/code><\/pre>\n<p>\u5728\u9019\u500b\u4f8b\u5b50\u4e2d\uff0c<code>FOR product_id IN (SELECT product_id FROM products)<\/code> \u662f\u4e00\u500b\u975e\u5e38\u91cf\u8868\u9054\u5f0f\uff0c\u9019\u5c07\u5c0e\u81f4 <strong>ORA-56901<\/strong> \u932f\u8aa4\u3002<\/p>\n<h2 id=\"%e8%a7%a3%e6%b1%ba%e6%96%b9%e6%a1%88-AFJXQaIvUx\">\u89e3\u6c7a\u65b9\u6848<\/h2>\n<p>\u8981\u89e3\u6c7a <strong>ORA-56901<\/strong> \u932f\u8aa4\uff0c\u60a8\u9700\u8981\u78ba\u4fdd\u5728 <code>PIVOT<\/code> \u6216 <code>UNPIVOT<\/code> \u64cd\u4f5c\u4e2d\u4f7f\u7528\u7684\u6240\u6709\u503c\u90fd\u662f\u5e38\u91cf\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u89e3\u6c7a\u65b9\u6848\uff1a<\/p>\n<ul>\n<li><strong>\u4f7f\u7528\u975c\u614b\u503c\uff1a<\/strong>\u76f4\u63a5\u5728 <code>IN<\/code> \u5b50\u53e5\u4e2d\u5217\u51fa\u6240\u6709\u53ef\u80fd\u7684\u503c\u3002<\/li>\n<li><strong>\u4f7f\u7528\u5b50\u67e5\u8a62\u7684\u7d50\u679c\uff1a<\/strong>\u5982\u679c\u9700\u8981\u52d5\u614b\u751f\u6210\u503c\uff0c\u53ef\u4ee5\u5148\u5c07\u7d50\u679c\u5b58\u5132\u5728\u4e00\u500b\u81e8\u6642\u8868\u4e2d\uff0c\u7136\u5f8c\u518d\u4f7f\u7528\u9019\u500b\u81e8\u6642\u8868\u9032\u884c <code>PIVOT<\/code> \u64cd\u4f5c\u3002<\/li>\n<\/ul>\n<h3 id=\"%e7%a4%ba%e4%be%8b%e4%bf%ae%e5%be%a9-AFJXQaIvUx\">\u793a\u4f8b\u4fee\u5fa9<\/h3>\n<p>\u4ee5\u4e0b\u662f\u4fee\u5fa9\u4e0a\u8ff0\u793a\u4f8b\u7684\u4ee3\u78bc\uff1a<\/p>\n<pre><code>SELECT *\nFROM sales\nPIVOT (\n    SUM(amount)\n    FOR product_id IN (1, 2, 3)  -- \u4f7f\u7528\u975c\u614b\u503c\n);\n<\/code><\/pre>\n<p>\u5728\u9019\u500b\u4fee\u5fa9\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u5011\u76f4\u63a5\u5217\u51fa\u4e86 <code>product_id<\/code> \u7684\u975c\u614b\u503c\uff0c\u9019\u6a23\u5c31\u4e0d\u6703\u5f15\u767c <strong>ORA-56901<\/strong> \u932f\u8aa4\u3002<\/p>\n<h2 id=\"%e9%81%a0%e7%a8%8b%e8%99%95%e7%90%86-AFJXQaIvUx\">\u9060\u7a0b\u8655\u7406<\/h2>\n<p>\u5728\u9047\u5230 <strong>ORA-56901<\/strong> \u932f\u8aa4\u6642\uff0c\u9060\u7a0b\u8655\u7406\u53ef\u4ee5\u662f\u4e00\u500b\u6709\u6548\u7684\u89e3\u6c7a\u65b9\u6848\u3002\u901a\u904e\u9060\u7a0b\u9023\u63a5\u5230\u6578\u64da\u5eab\uff0c\u60a8\u53ef\u4ee5\u5feb\u901f\u6aa2\u67e5 SQL \u67e5\u8a62\uff0c\u4e26\u9032\u884c\u5fc5\u8981\u7684\u8abf\u6574\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u9060\u7a0b\u8655\u7406\u7684\u6b65\u9a5f\uff1a<\/p>\n<ul>\n<li><strong>\u9023\u63a5\u5230\u6578\u64da\u5eab\uff1a<\/strong>\u4f7f\u7528 SQL \u5ba2\u6236\u7aef\u5de5\u5177\uff08\u5982 SQL Developer \u6216 TOAD\uff09\u9023\u63a5\u5230 Oracle \u6578\u64da\u5eab\u3002<\/li>\n<li><strong>\u6aa2\u67e5 SQL \u67e5\u8a62\uff1a<\/strong>\u67e5\u770b\u5f15\u767c\u932f\u8aa4\u7684 SQL \u67e5\u8a62\uff0c\u78ba\u4fdd\u6240\u6709\u7684\u8f49\u63db\u503c\u90fd\u662f\u5e38\u91cf\u3002<\/li>\n<li><strong>\u6e2c\u8a66\u4fee\u5fa9\uff1a<\/strong>\u5728\u4fee\u6539\u5f8c\u7684\u67e5\u8a62\u4e0a\u9032\u884c\u6e2c\u8a66\uff0c\u78ba\u4fdd\u4e0d\u518d\u51fa\u73fe\u932f\u8aa4\u3002<\/li>\n<\/ul>\n<h2 id=\"%e7%b8%bd%e7%b5%90-AFJXQaIvUx\">\u7e3d\u7d50<\/h2>\n<p>\u5728 Oracle \u6578\u64da\u5eab\u4e2d\uff0c<strong>ORA-56901<\/strong> \u932f\u8aa4\u901a\u5e38\u662f\u7531\u65bc\u4f7f\u7528\u4e86\u975e\u5e38\u91cf\u8868\u9054\u5f0f\u800c\u5f15\u8d77\u7684\u3002\u901a\u904e\u78ba\u4fdd\u5728 <code>PIVOT<\/code> \u548c <code>UNPIVOT<\/code> \u64cd\u4f5c\u4e2d\u4f7f\u7528\u5e38\u91cf\u503c\uff0c\u53ef\u4ee5\u6709\u6548\u907f\u514d\u6b64\u932f\u8aa4\u3002\u82e5\u9700\u9032\u4e00\u6b65\u7684\u6280\u8853\u652f\u6301\u6216\u89e3\u6c7a\u65b9\u6848\uff0c\u60a8\u53ef\u4ee5\u8003\u616e\u4f7f\u7528 <a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a> \u670d\u52d9\uff0c\u9019\u5c07\u70ba\u60a8\u7684\u6578\u64da\u5eab\u7ba1\u7406\u63d0\u4f9b\u7a69\u5b9a\u7684\u74b0\u5883\u548c\u9ad8\u6548\u7684\u6027\u80fd\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u89e3\u6c7a ORA-56901 \u932f\u8aa4\uff0c\u4e86\u89e3\u4e0d\u5141\u8a31\u5c0d pivot|unpivot \u503c\u4f7f\u7528\u975e\u5e38\u91cf\u8868\u9054\u5f0f\u7684\u539f\u56e0\u53ca\u9060\u7a0b\u8655\u7406\u65b9\u6cd5\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-93622","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/93622","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=93622"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/93622\/revisions"}],"predecessor-version":[{"id":93623,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/93622\/revisions\/93623"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=93622"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=93622"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=93622"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}