Sql 从oracle表生成XML文件格式
我想要如下所示的XML文件格式 XML应该如下所示:-Sql 从oracle表生成XML文件格式,sql,xml,oracle,Sql,Xml,Oracle,我想要如下所示的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
<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>
谢谢你的帮助。
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;
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
)
| 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> |
查询1:
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;
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
)
| 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> |
:
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;
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
)
| 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> |
| XML|
|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| |
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;
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
)
| 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> |
查询1:
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;
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
)
| 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> |
:
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;
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
)
| 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> |
| XML|
|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| |
“我很想使用dbms\u xmlgen软件包,但我不知道如何实现上述目标。”因此,请阅读文档,尝试一些方法,如果不起作用,请发布一个具体问题。请。告诉我们您尝试过什么?我建议使用另一种编程语言将查询结果转换为xml。@DanBracuk为什么要在Oracle可以轻松完成转换的情况下使用另一种语言?“我本来想使用包dbms_xmlgen,但我不知道如何实现上述目标。”因此,请阅读文档,尝试一些方法,如果它不起作用,发布一个具体的问题。告诉我们您尝试了什么?我建议使用另一种编程语言将查询结果转换为xml。@DanBracuk为什么要使用另一种语言,而Oracle可以轻松地进行转换?