{"id":88356,"date":"2024-10-18T08:45:20","date_gmt":"2024-10-18T00:45:20","guid":{"rendered":"https:\/\/server.hk\/cnblog\/88356\/"},"modified":"2024-10-18T08:45:20","modified_gmt":"2024-10-18T00:45:20","slug":"ora-24347-%e8%ad%a6%e5%91%8a%ef%bc%9a%e8%81%9a%e5%90%88%e5%87%bd%e6%95%b8%e4%b8%ad%e7%9a%84-null-%e5%88%97-oracle-%e5%a0%b1%e9%8c%af-%e6%95%85%e9%9a%9c%e4%bf%ae%e5%be%a9-%e9%81%a0%e7%a8%8b%e8%99%95","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/88356\/","title":{"rendered":"ORA-24347: \u8b66\u544a\uff1a\u805a\u5408\u51fd\u6578\u4e2d\u7684 NULL \u5217 ORACLE \u5831\u932f \u6545\u969c\u4fee\u5fa9 \u9060\u7a0b\u8655\u7406"},"content":{"rendered":"<h1 id=\"ora-24347-%e8%ad%a6%e5%91%8a%ef%bc%9a%e8%81%9a%e5%90%88%e5%87%bd%e6%95%b8%e4%b8%ad%e7%9a%84-null-%e5%88%97-oracle-%e5%a0%b1%e9%8c%af-%e6%95%85%e9%9a%9c%e4%bf%ae%e5%be%a9-%e9%81%a0%e7%a8%8b%e8%99%95-UKlRHCBAwg\">ORA-24347: \u8b66\u544a\uff1a\u805a\u5408\u51fd\u6578\u4e2d\u7684 NULL \u5217 ORACLE \u5831\u932f \u6545\u969c\u4fee\u5fa9 \u9060\u7a0b\u8655\u7406<\/h1>\n<p>\u5728\u4f7f\u7528 Oracle \u6578\u64da\u5eab\u6642\uff0c\u958b\u767c\u8005\u548c\u6578\u64da\u5eab\u7ba1\u7406\u54e1\u53ef\u80fd\u6703\u9047\u5230\u5404\u7a2e\u932f\u8aa4\u4ee3\u78bc\uff0c\u5176\u4e2d\u4e4b\u4e00\u4fbf\u662f <code>ORA-24347<\/code>\u3002\u9019\u500b\u932f\u8aa4\u901a\u5e38\u8207\u805a\u5408\u51fd\u6578\u7684\u4f7f\u7528\u6709\u95dc\uff0c\u7279\u5225\u662f\u5728\u8655\u7406 NULL \u503c\u6642\u3002\u672c\u6587\u5c07\u6df1\u5165\u63a2\u8a0e\u9019\u500b\u932f\u8aa4\u7684\u539f\u56e0\u3001\u5f71\u97ff\u4ee5\u53ca\u5982\u4f55\u9032\u884c\u6545\u969c\u4fee\u5fa9\u3002<\/p>\n<h2 id=\"ora-24347-%e9%8c%af%e8%aa%a4%e7%9a%84%e5%8e%9f%e5%9b%a0-UKlRHCBAwg\">ORA-24347 \u932f\u8aa4\u7684\u539f\u56e0<\/h2>\n<p>\u7576\u4f60\u5728 SQL \u67e5\u8a62\u4e2d\u4f7f\u7528\u805a\u5408\u51fd\u6578\uff08\u5982 <code>SUM<\/code>\u3001<code>AVG<\/code>\u3001<code>COUNT<\/code> \u7b49\uff09\u6642\uff0c\u5982\u679c\u805a\u5408\u51fd\u6578\u7684\u53c3\u6578\u4e2d\u5305\u542b NULL \u503c\uff0cOracle \u53ef\u80fd\u6703\u8fd4\u56de <code>ORA-24347<\/code> \u932f\u8aa4\u3002\u9019\u662f\u56e0\u70ba\u805a\u5408\u51fd\u6578\u5728\u8a08\u7b97\u6642\u7121\u6cd5\u6b63\u78ba\u8655\u7406 NULL \u503c\uff0c\u5c0e\u81f4\u7d50\u679c\u4e0d\u6b63\u78ba\u3002<\/p>\n<h3 id=\"%e5%b8%b8%e8%a6%8b%e5%a0%b4%e6%99%af-UKlRHCBAwg\">\u5e38\u898b\u5834\u666f<\/h3>\n<ul>\n<li>\u5728\u8a08\u7b97\u5e73\u5747\u503c\u6642\uff0c\u67d0\u4e9b\u884c\u7684\u6578\u64da\u70ba NULL\u3002<\/li>\n<li>\u5728\u8a08\u7b97\u7e3d\u548c\u6642\uff0c\u67d0\u4e9b\u884c\u7684\u6578\u64da\u70ba NULL\u3002<\/li>\n<li>\u4f7f\u7528 <code>GROUP BY<\/code> \u5b50\u53e5\u6642\uff0c\u67d0\u4e9b\u5206\u7d44\u7684\u6578\u64da\u70ba NULL\u3002<\/li>\n<\/ul>\n<h2 id=\"%e5%a6%82%e4%bd%95%e4%bf%ae%e5%be%a9-ora-24347-%e9%8c%af%e8%aa%a4-UKlRHCBAwg\">\u5982\u4f55\u4fee\u5fa9 ORA-24347 \u932f\u8aa4<\/h2>\n<p>\u4fee\u5fa9 <code>ORA-24347<\/code> \u932f\u8aa4\u7684\u7b2c\u4e00\u6b65\u662f\u78ba\u5b9a\u54ea\u4e9b\u5217\u5305\u542b NULL \u503c\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u7528\u7684\u65b9\u6cd5\u4f86\u8655\u7406\u9019\u500b\u554f\u984c\uff1a<\/p>\n<h3 id=\"1-%e4%bd%bf%e7%94%a8-nvl-%e5%87%bd%e6%95%b8-UKlRHCBAwg\">1. \u4f7f\u7528 NVL \u51fd\u6578<\/h3>\n<p>Oracle \u63d0\u4f9b\u4e86 <code>NVL<\/code> \u51fd\u6578\uff0c\u53ef\u4ee5\u5c07 NULL \u503c\u66ff\u63db\u70ba\u5176\u4ed6\u503c\u3002\u9019\u6a23\u53ef\u4ee5\u907f\u514d\u5728\u8a08\u7b97\u805a\u5408\u51fd\u6578\u6642\u51fa\u73fe\u932f\u8aa4\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code>SELECT AVG(NVL(salary, 0)) AS average_salary\nFROM employees;<\/code><\/pre>\n<p>\u5728\u9019\u500b\u4f8b\u5b50\u4e2d\uff0c\u6240\u6709\u7684 NULL \u5de5\u8cc7\u5c07\u88ab\u66ff\u63db\u70ba 0\uff0c\u5f9e\u800c\u907f\u514d\u4e86\u8a08\u7b97\u5e73\u5747\u5de5\u8cc7\u6642\u51fa\u73fe\u7684\u932f\u8aa4\u3002<\/p>\n<h3 id=\"2-%e4%bd%bf%e7%94%a8-coalesce-%e5%87%bd%e6%95%b8-UKlRHCBAwg\">2. \u4f7f\u7528 COALESCE \u51fd\u6578<\/h3>\n<p>\u53e6\u4e00\u500b\u9078\u64c7\u662f\u4f7f\u7528 <code>COALESCE<\/code> \u51fd\u6578\uff0c\u5b83\u53ef\u4ee5\u63a5\u53d7\u591a\u500b\u53c3\u6578\u4e26\u8fd4\u56de\u7b2c\u4e00\u500b\u975e NULL \u7684\u503c\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code>SELECT SUM(COALESCE(salary, 0)) AS total_salary\nFROM employees;<\/code><\/pre>\n<p>\u9019\u6a23\u53ef\u4ee5\u78ba\u4fdd\u5728\u8a08\u7b97\u7e3d\u5de5\u8cc7\u6642\u4e0d\u6703\u56e0\u70ba NULL \u503c\u800c\u5c0e\u81f4\u932f\u8aa4\u3002<\/p>\n<h3 id=\"3-%e6%aa%a2%e6%9f%a5%e6%95%b8%e6%93%9a%e5%ae%8c%e6%95%b4%e6%80%a7-UKlRHCBAwg\">3. \u6aa2\u67e5\u6578\u64da\u5b8c\u6574\u6027<\/h3>\n<p>\u9664\u4e86\u4f7f\u7528\u51fd\u6578\u4f86\u8655\u7406 NULL \u503c\u5916\uff0c\u9084\u61c9\u8a72\u6aa2\u67e5\u6578\u64da\u7684\u5b8c\u6574\u6027\u3002\u78ba\u4fdd\u5728\u63d2\u5165\u6578\u64da\u6642\uff0c\u5fc5\u8981\u7684\u5b57\u6bb5\u4e0d\u61c9\u8a72\u70ba NULL\u3002\u53ef\u4ee5\u901a\u904e\u8a2d\u7f6e\u7d04\u675f\u4f86\u9632\u6b62\u9019\u7a2e\u60c5\u6cc1\u7684\u767c\u751f\uff1a<\/p>\n<pre><code>ALTER TABLE employees\nADD CONSTRAINT salary_not_null CHECK (salary IS NOT NULL);<\/code><\/pre>\n<h2 id=\"%e9%81%a0%e7%a8%8b%e8%99%95%e7%90%86-ora-24347-%e9%8c%af%e8%aa%a4-UKlRHCBAwg\">\u9060\u7a0b\u8655\u7406 ORA-24347 \u932f\u8aa4<\/h2>\n<p>\u5728\u67d0\u4e9b\u60c5\u6cc1\u4e0b\uff0c\u53ef\u80fd\u9700\u8981\u9060\u7a0b\u8655\u7406 <code>ORA-24347<\/code> \u932f\u8aa4\u3002\u9019\u53ef\u4ee5\u901a\u904e\u4ee5\u4e0b\u6b65\u9a5f\u4f86\u5be6\u73fe\uff1a<\/p>\n<ul>\n<li>\u4f7f\u7528\u9060\u7a0b\u684c\u9762\u9023\u63a5\u5230\u6578\u64da\u5eab\u4f3a\u670d\u5668\u3002<\/li>\n<li>\u6aa2\u67e5 SQL \u67e5\u8a62\u7684\u57f7\u884c\u8a08\u5283\uff0c\u78ba\u4fdd\u805a\u5408\u51fd\u6578\u7684\u4f7f\u7528\u662f\u6b63\u78ba\u7684\u3002<\/li>\n<li>\u6839\u64da\u9700\u8981\u8abf\u6574\u67e5\u8a62\uff0c\u4f7f\u7528 <code>NVL<\/code> \u6216 <code>COALESCE<\/code> \u51fd\u6578\u3002<\/li>\n<\/ul>\n<h2 id=\"%e7%b8%bd%e7%b5%90-UKlRHCBAwg\">\u7e3d\u7d50<\/h2>\n<p>\u5728\u4f7f\u7528 Oracle \u6578\u64da\u5eab\u6642\uff0c<code>ORA-24347<\/code> \u932f\u8aa4\u53ef\u80fd\u6703\u5f71\u97ff\u6578\u64da\u7684\u6b63\u78ba\u6027\u548c\u67e5\u8a62\u7684\u57f7\u884c\u3002\u901a\u904e\u4f7f\u7528 <code>NVL<\/code> \u548c <code>COALESCE<\/code> \u51fd\u6578\u4f86\u8655\u7406 NULL \u503c\uff0c\u4ee5\u53ca\u6aa2\u67e5\u6578\u64da\u5b8c\u6574\u6027\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u4fee\u5fa9\u9019\u500b\u932f\u8aa4\u3002\u6b64\u5916\uff0c\u5c0d\u65bc\u9700\u8981\u9060\u7a0b\u8655\u7406\u7684\u60c5\u6cc1\uff0c\u78ba\u4fdd\u6709\u9069\u7576\u7684\u9023\u63a5\u548c\u6b0a\u9650\uff0c\u4ee5\u4fbf\u9032\u884c\u5fc5\u8981\u7684\u8abf\u6574\u3002<\/p>\n<p>\u5982\u9700\u4e86\u89e3\u66f4\u591a\u6709\u95dc <a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a> \u548c\u6578\u64da\u5eab\u7ba1\u7406\u7684\u8cc7\u8a0a\uff0c\u8acb\u8a2a\u554f\u6211\u5011\u7684\u7db2\u7ad9\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u89e3\u6c7a ORA-24347 \u8b66\u544a\uff1a\u805a\u5408\u51fd\u6578\u4e2d\u7684 NULL \u5217\u554f\u984c\uff0c\u63d0\u4f9b\u6545\u969c\u4fee\u5fa9\u548c\u9060\u7a0b\u8655\u7406\u65b9\u6848\uff0c\u52a9\u60a8\u5feb\u901f\u6062\u5fa9\u7cfb\u7d71\u904b\u884c\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-88356","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/88356","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=88356"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/88356\/revisions"}],"predecessor-version":[{"id":88357,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/88356\/revisions\/88357"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=88356"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=88356"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=88356"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}