{"id":3299,"date":"2020-06-16T10:03:51","date_gmt":"2020-06-16T02:03:51","guid":{"rendered":"https:\/\/nj.transwarp.cn:8180\/?p=3299"},"modified":"2020-06-16T10:03:51","modified_gmt":"2020-06-16T02:03:51","slug":"oracle-ddl%e8%bd%ac%e6%8d%a2%e6%88%90inceptor-ddl%ef%bc%88utl_file%e6%96%b9%e5%bc%8f%ef%bc%89","status":"publish","type":"post","link":"https:\/\/kbwp.transwarp.cn\/?p=3299","title":{"rendered":"Oracle DDL\u8f6c\u6362\u6210Inceptor DDL\uff08Utl_file\u65b9\u5f0f\uff09"},"content":{"rendered":"<h3>\u6982\u8981\u63cf\u8ff0<\/h3>\n<hr \/>\n<p>\u6570\u636e\u7ed3\u6784\u4eceOracle \u8fc1\u79fb\u5230 Inceptor\u7684\u8fc7\u7a0b\u4e2d\uff0c\u7ecf\u5e38\u9047\u5230\u65e0\u6cd5\u901a\u8fc7\u5de5\u5177\u76f4\u63a5\u5bfc\u5165 Inceptor \u7684\u95ee\u9898\uff0c\u9700\u8981\u6279\u91cf\u5bf9 Oracle \u7684 DDL \u8fdb\u884c\u8f6c\u6362\uff0c\u8f6c\u6362\u6210 Inceptor \u80fd\u591f\u8bc6\u522b\u7684 DDL \u8bed\u53e5\uff0c\u7531\u4e8e\u6211\u4eec\u53ea\u9700\u8981 inceptor \u5916\u8868\uff0c\u6240\u4ee5\u8fc7\u7a0b\u4e2d\u91cd\u70b9\u5173\u6ce8\u5b57\u6bb5\u7c7b\u578b\u6620\u5c04\u3001location \u8def\u5f84\u6307\u5b9a\u3002<\/p>\n<h3>\u8be6\u7ec6\u8bf4\u660e<\/h3>\n<hr \/>\n<p>\u5b9e\u9645\u7684\u539f\u7406\u662f\u901a\u8fc7\u52a8\u6001\u62fc\u63a5 DDL \u8bed\u53e5\uff0c\u5176\u4e2d\u9047\u5230\u6570\u636e\u7c7b\u578b\u548c Inceptor \u4e0d\u5339\u914d\u7684\uff0c\u8fdb\u884c\u6570\u636e\u7c7b\u578b\u8f6c\u6362\u5373\u53ef\u3002<\/p>\n<h4>1. \u5bf9\u7528\u6237\u6dfb\u52a0Utl_file\u6743\u9650<\/h4>\n<pre><code class=\"language-sql\">--\u5b9a\u4e49\u751f\u6210\u6587\u672c\u7684\u8def\u5f84\n> create or replace directory \"FILEPATH\" as 'D:\/oracle';\n> grant read,write on directory FILEPATH to lkw;\n> grant execute on utl_file to lkw;<\/code><\/pre>\n<h4>2. \u6267\u884c\u8bed\u53e5\u5757<\/h4>\n<p><strong><font color=red>\u6ce8\u610f\uff1a\u5982\u679c\u53d1\u73b0\u751f\u6210\u7684 DDL \u4e2d\u6ca1\u6709\u6570\u636e\u7c7b\u578b\uff0c\u53ef\u4ee5\u53c2\u8003 Oracle \u548c Inceptor\u7684\u5bf9\u5e94\u5173\u7cfb\u81ea\u884c\u6dfb\u52a0 case when \u5224\u65ad<\/font><\/strong><\/p>\n<pre><code class=\"language-sql\">--\u539f\u7406\u5176\u5b9e\u5c31\u662f\u52a8\u6001\u62fc\u63a5DDL\u8bed\u53e5\uff0c\u5728user_tab_columns\u5bf9\u6570\u636e\u7c7b\u578b\u8fdb\u884c\u8f6c\u6362\uff0c\u7136\u540e\u751f\u6210\u7684\u6587\u672c\u8f93\u51fa\u5230oracle\u670d\u52a1\u5668\u4e0a\n--\u5982\u679c\u60f3\u6307\u5b9a\u5bfc\u51fa\u7684\u8868\uff0c\u53ef\u4ee5\u5728user_tables\u8fd9\u4e2afor\u5faa\u73af\u4e0a\u52a0where\u6761\u4ef6\n--oracle\u4e0einceptor\u5b57\u6bb5\u7c7b\u578b\u6620\u5c04\uff0c\u53c2\u8003inceptor\u624b\u518c\u4e2d\u7684\"\u6570\u636e\u7c7b\u578b\u5bf9\u5e94\u8868\"\ndeclare\n  v_sql      varchar2(4000);\n  OutputFile utl_file.file_type;\nbegin\n  OutputFile := utl_file.fopen('FILEPATH', 'target.sql', 'W');\n  for tb_name in (select table_name from user_tables) loop\n    select 'create external table ' || tb_name.table_name || '(' ||wmsys.wm_concat(COLUMN_NAME || ' ' || dtype) || ') location ''\/oracle\/' || tb_name.table_name || ''';'\n      into v_sql\n      from (select t.TABLE_NAME,\n                   t.COLUMN_NAME,\n                   case data_type\n                     when 'NUMBER' then\n                      ' decimal(' || data_length || ',' || nvl(data_scale, 0) || ') '\n                     when 'FLOAT' then\n                      ' decimal(' || data_length || ',' || nvl(data_scale, 0) || ') '\n                     when 'VARCHAR2' then\n                      ' string '\n                     when 'DATE' then\n                      ' string '\n                     when 'INTEGER' then\n                      ' decimal(38,0)'\n                     when 'CHAR' then\n                      ' string '\n                     when 'BLOB' then\n                      ' binary '\n                     when 'CLOB' then\n                      ' binary '\n                   end dtype\n              from user_tab_columns t\n             where table_name = tb_name.table_name\n             order by COLUMN_ID asc);\n    utl_file.put_line(OutputFile, V_SQL);\n  end loop;\n  utl_file.fclose(OutputFile);\nend;\n\/<\/code><\/pre>\n<h4>3. \u5230oracle\u670d\u52a1\u5668\u83b7\u53d6 D:\/oracle\/target.sql<\/h4>\n<p>\u68c0\u67e5 D:\/oracle\/target.sql \u7684\u5185\u5bb9\uff0c\u5982\u4e0b\u56fe\u6240\u793a\uff1a<\/p>\n<p><img decoding=\"async\" src=\"\/wp-content\/uploads\/2020\/06\/image-1591858378497.png\" alt=\"file\" \/><\/p>\n<h4>4. \u901a\u8fc7beeline -f\u7684\u5f62\u5f0f\u6267\u884c\u8be5 sql \u6587\u672c\u5b8c\u6210 DDL \u8fc1\u79fb<\/h4>\n<p><i class=\"fas fa-user-friends fa-2x\"><\/i>  <strong>\u5982\u679c\u60a8\u5728\u4f7f\u7528\u4e2d\u6709\u4efb\u4f55\u7591\u95ee\uff0c\u53ef\u4ee5\u968f\u65f6\u901a\u8fc7 <a href=\"https:\/\/nj.transwarp.cn:8180\/?p=641\" title=\"\u552e\u540e\u6e20\u9053\">\u552e\u540e\u6e20\u9053<\/a>  \u8054\u7cfb\u6211\u4eec\uff0c\u611f\u8c22\u60a8\u7684\u7406\u89e3\u4e0e\u652f\u6301\uff01<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6982\u8981\u63cf\u8ff0 \u6570\u636e\u7ed3\u6784\u4eceOracle \u8fc1\u79fb\u5230 Inceptor\u7684\u8fc7\u7a0b\u4e2d\uff0c\u7ecf\u5e38\u9047\u5230\u65e0\u6cd5\u901a\u8fc7\u5de5\u5177\u76f4\u63a5\u5bfc\u5165 Incept ..<\/p>\n<div class=\"clear-fix\"><\/div>\n<p><a href=\"https:\/\/kbwp.transwarp.cn\/?p=3299\" 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":[36],"tags":[],"class_list":["post-3299","post","type-post","status-publish","format-standard","hentry","category-migration"],"acf":[],"_links":{"self":[{"href":"https:\/\/kbwp.transwarp.cn\/index.php?rest_route=\/wp\/v2\/posts\/3299","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=3299"}],"version-history":[{"count":3,"href":"https:\/\/kbwp.transwarp.cn\/index.php?rest_route=\/wp\/v2\/posts\/3299\/revisions"}],"predecessor-version":[{"id":3370,"href":"https:\/\/kbwp.transwarp.cn\/index.php?rest_route=\/wp\/v2\/posts\/3299\/revisions\/3370"}],"wp:attachment":[{"href":"https:\/\/kbwp.transwarp.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3299"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kbwp.transwarp.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3299"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kbwp.transwarp.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3299"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}