{"id":2123,"date":"2021-09-07T09:33:08","date_gmt":"2021-09-07T01:33:08","guid":{"rendered":"https:\/\/nj.transwarp.cn:8180\/?p=2123"},"modified":"2021-09-16T10:13:59","modified_gmt":"2021-09-16T02:13:59","slug":"sql%e4%b8%8d%e6%94%af%e6%8c%81%e8%b0%83%e7%94%a8%e5%8c%85%e5%90%ab%e6%99%ae%e9%80%9asql%e5%92%8cset_env%e7%9a%84function","status":"publish","type":"post","link":"https:\/\/kbwp.transwarp.cn\/?p=2123","title":{"rendered":"sql\u4e0d\u652f\u6301\u8c03\u7528\u5305\u542b\u6807\u51c6sql\u548cset_env\u7684function"},"content":{"rendered":"<h3>\u6982\u8981\u63cf\u8ff0<\/h3>\n<hr \/>\n<p>\u4e0a\u7ed3\u8bba\uff1a<\/p>\n<p><strong>function \u7684 select sql \u8c03\u7528\u4e0b\uff0c\u662f\u4e0d\u652f\u6301 function \u5185\u5bb9\u4e2d\u6709\u201c\u6807\u51c6sql\u201d\u548c\u201cset_env\u201d\u8fd9\u4e24\u4e2a\u4e1c\u897f\u7684\u3002<\/strong><\/p>\n<h3>\u8be6\u7ec6\u8bf4\u660e<\/h3>\n<hr \/>\n<h4>1\u3001function\u4e2d\u5305\u542b\u6807\u51c6sql<\/h4>\n<p><strong>function DDL:<\/strong><\/p>\n<pre><code class=\"language-sql\">create or replace function test_sql()\n    RETURN date\nIS\n    v1 DATE;\nBEGIN\n    select sysdate into v1 from system.dual;\n    RETURN v1\nEND<\/code><\/pre>\n<p><strong>pl\/sql\u8c03\u7528\u4e0b\uff0c\u6b63\u5e38\uff1a<\/strong><\/p>\n<pre><code class=\"language-sql\">DECLARE\n    v_date DATE;\nBEGIN\n    v_date:=test_sql()\n    dbms_output.put_line(v_date)\nEND<\/code><\/pre>\n<p><img decoding=\"async\" src=\"\/wp-content\/uploads\/2021\/09\/image-1630568307275.png\" alt=\"file\" \/><\/p>\n<p><font color=red><em>\u6ce8\u610f\uff1apl\/sql\u4e0b\u4e5f\u662f\u7981\u6b62sql\u8c03\u7528\u5305\u542b\u6807\u51c6sql\u7684function\u7684\uff0c\u81f3\u4e8e\u9700\u8981sql\u8c03\u7528\u5e26sql\u7684plsql\u7684\u60c5\u51b5\uff0c\u4e1a\u52a1\u4e0a\u5efa\u8bae\u5728\u5b58\u50a8\u8fc7\u7a0b\u91cc\u9762\u505a\u5173\u8054\u67e5\u8be2\u6216\u8005\u5d4c\u5957\u5b50\u67e5\u8be2\u7684\u65b9\u5f0f\u3002\u6216\u8005\u53ef\u4ee5\u8003\u8651\u5982\u4e0b\u7684\u65b9\u5f0fworkaround\uff08\u591a\u503c\u7684\u8bdd\u9700\u8981\u4e0a\u6e38\u6807\uff09\uff1a<\/em><\/font><\/p>\n<p><img decoding=\"async\" src=\"\/wp-content\/uploads\/2021\/09\/image-1631758338733.png\" alt=\"file\" \/><\/p>\n<p><strong>\u666e\u901asql\u8c03\u7528\u4e0b\uff0c\u5f02\u5e38\uff1a<\/strong><\/p>\n<p><font color=red>SQL \u9519\u8bef [30028] [42000]: COMPILE FAILED: Semantic error: [Error 30028] Line 1:7 PLSQL function is running in a non-driver environment, usually in SQL statement which doesn&#8217;t allow nested SQL statement. Error encountered near token &#8216;test_sql&#8217;<\/font><\/p>\n<pre><code class=\"language-sql\">SELECT test_sql() FROM system.dual;<\/code><\/pre>\n<p><img decoding=\"async\" src=\"\/wp-content\/uploads\/2021\/09\/image-1630568350851.png\" alt=\"file\" \/><\/p>\n<h4>2\u3001function \u4e2d\u5305\u542b set_env<\/h4>\n<p><strong>function DDL:<\/strong><\/p>\n<pre><code class=\"language-sql\">create or replace function test_set(v1 STRING,v2 STRING)\n    RETURN STRING \nIS\nBEGIN\n    set_env('plsql.catch.hive.exception',TRUE);\n    RETURN 'A'\nEND<\/code><\/pre>\n<p><strong>pl\/sql\u8c03\u7528\u4e0b\uff0c\u6b63\u5e38\uff1a<\/strong><\/p>\n<pre><code class=\"language-sql\">DECLARE\n    v1 STRING;\n    v2 STRING;\n    v_out STRING;\nBEGIN\n    SELECT ENAME,JOB INTO v1,v2 FROM EMP_TORC WHERE ENAME='JONES'\n    v_out:=test_set(v1,v2)\n    dbms_output.put_line(v_out)\nEND<\/code><\/pre>\n<p><img decoding=\"async\" src=\"\/wp-content\/uploads\/2021\/09\/image-1630569102857.png\" alt=\"file\" \/><\/p>\n<p><strong>\u666e\u901asql\u8c03\u7528\u4e0b\uff0c\u5f02\u5e38\uff1a<\/strong><\/p>\n<p><font color=red>SQL \u9519\u8bef [1] [08S01]: EXECUTION FAILED: Task MAPRED-SPARK error SparkException: [Error 1] Job aborted due to stage failure: Task 0 in stage 99.0 failed 4 times, most recent failure: Lost task 0.3 in stage 99.0 (TID 49, 172.22.23.2): java.lang.RuntimeException: org.apache.hadoop.hive.ql.pl.exception.PLException: Hive exception<\/font><\/p>\n<pre><code class=\"language-sql\">SELECT test_set(ENAME,JOB) FROM EMP_TORC WHERE ENAME='JONES';<\/code><\/pre>\n<p><img decoding=\"async\" src=\"\/wp-content\/uploads\/2021\/09\/image-1630568920735.png\" alt=\"file\" \/><\/p>\n<p><strong>\u6b63\u5e38\u7684 call \u6216\u8005 begin end \u65b9\u5f0f\u8c03\u7528 function\uff0c\u662f\u5728 server \u7aef\u6267\u884c\u7684\uff0c\u50cf\u201c\u6807\u51c6sql\u201d\u548c\u201cset_env\u201d\u662f\u9700\u8981 server context \u6765\u6267\u884c\u7684\u3002<\/strong><br \/>\n<strong>\u800c sql \u91cc\u9762\u8c03\u7528 function \u7684\u8bdd\uff0c\u662f\u5728 executor \u7aef\u8dd1\u7684\uff0cexecutor \u7aef\u6ca1\u6709 server \u7684 context\uff0c\u65e0\u6cd5\u4ece\u5934\u5f00\u59cb\u7f16\u8bd1\u4f18\u5316\u6267\u884c sql\uff0c\u6240\u4ee5\u4f1a\u5931\u8d25\u3002<\/strong><\/p>\n<p><strong>\u540e\u9762\u5982\u679c\u9700\u8981\u7ed3\u5408\u4e00\u4e9b set \u547d\u4ee4\u6765\u6267\u884c sql \u8bed\u53e5\u7684 function \u8bdd\uff0c\u5efa\u8bae\u5148\u5728 session \u91cc\u9762 set\uff0c\u7136\u540e\u518d sql \u8c03\u7528 function\u3002<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6982\u8981\u63cf\u8ff0 \u4e0a\u7ed3\u8bba\uff1a function \u7684 select sql \u8c03\u7528\u4e0b\uff0c\u662f\u4e0d\u652f\u6301 function \u5185\u5bb9\u4e2d\u6709\u201c ..<\/p>\n<div class=\"clear-fix\"><\/div>\n<p><a href=\"https:\/\/kbwp.transwarp.cn\/?p=2123\" title=\"read more...\">Read more<\/a><\/p>\n","protected":false},"author":12,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[40],"tags":[],"class_list":["post-2123","post","type-post","status-publish","format-standard","hentry","category-sql_issue"],"acf":[],"_links":{"self":[{"href":"https:\/\/kbwp.transwarp.cn\/index.php?rest_route=\/wp\/v2\/posts\/2123","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kbwp.transwarp.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kbwp.transwarp.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kbwp.transwarp.cn\/index.php?rest_route=\/wp\/v2\/users\/12"}],"replies":[{"embeddable":true,"href":"https:\/\/kbwp.transwarp.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2123"}],"version-history":[{"count":2,"href":"https:\/\/kbwp.transwarp.cn\/index.php?rest_route=\/wp\/v2\/posts\/2123\/revisions"}],"predecessor-version":[{"id":6764,"href":"https:\/\/kbwp.transwarp.cn\/index.php?rest_route=\/wp\/v2\/posts\/2123\/revisions\/6764"}],"wp:attachment":[{"href":"https:\/\/kbwp.transwarp.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2123"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kbwp.transwarp.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2123"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kbwp.transwarp.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2123"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}