【问题标题】:gernerate XML file format from oracle Table从 oracle Table 生成 XML 文件格式
【发布时间】:2017-09-25 12:56:46
【问题描述】:

我想要一个如下的 XML 文件格式。

XML 应该如下所示:-

<server name="Server123">
<schema name="cwmm">
<table name="ACC" rows="1000000"/>
<table name="KEYS" rows="1000000"/>
</schema>
<schema name="CWM1610">
<table name="ACC" rows="1000000"/>
<table name="KEYS" rows="1000000"/>
</schema>

我所做的是创建了一个名为 TAB_INFO 的临时表,其中包含有关 table_owner、表及其行的信息。所以请帮助实现上述格式。我正在考虑使用包 dbms_xmlgen 但我不知道如何实现上述目标。

表格的列如下:-

owner
table_name
num_rows

感谢您的帮助。

【问题讨论】:

  • “我想使用包 dbms_xmlgen,但我不知道如何实现上述目标。”所以阅读文档,尝试一些东西,如果它不起作用,发布一个特定的问题。
  • 请。告诉我们你尝试了什么?
  • 我建议使用另一种编程语言将您的查询结果转换为xml。
  • @DanBracuk 既然 Oracle 可以轻松完成转换,为什么还要使用另一种语言?

标签: sql xml oracle


【解决方案1】:

SQL Fiddle

Oracle 11g R2 架构设置

CREATE TABLE TAB_INFO (owner, table_name, num_rows ) AS
SELECT 'cwmm',    'ACC',  1000000 FROM DUAL UNION ALL
SELECT 'cwmm',    'KEYS', 1000000 FROM DUAL UNION ALL
SELECT 'CWM1610', 'ACC',  1000000 FROM DUAL UNION ALL
SELECT 'CWM1610', 'KEYS', 1000000 FROM DUAL;

查询 1

SELECT XMLElement(
         "server",
         XMLAttributes( 'Server123' AS "name" ),
         XMLAGG( schema )
       ).getClobVal() AS xml
FROM   (
  SELECT XMLElement(
           "schema",
           XMLAttributes( owner AS "name" ),
           XMLAgg(
             XMLElement(
               "table",
               XMLAttributes(
                 table_name AS "name",
                 num_rows AS "rows"
               )
             )
           )
         ) AS schema
  FROM   tab_info
  GROUP BY owner
)

Results

|                                                                                                                                                                                                                                                                   XML |
|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| <server name="Server123"><schema name="CWM1610"><table name="ACC" rows="1000000"></table><table name="KEYS" rows="1000000"></table></schema><schema name="cwmm"><table name="ACC" rows="1000000"></table><table name="KEYS" rows="1000000"></table></schema></server> |

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-14
    • 1970-01-01
    • 2014-03-22
    • 2016-03-05
    相关资源
    最近更新 更多