我在Oracle 11g中使用XMLElement
和XMLAgg
函数来生成XML String。然后将此XML String发送到另一个系统进行解析。我想将以下文本附加到我的XML结果中。
问题是,如果按原样附加文本,SQL语句将返回以下内容 -
<ZAVACOR_RESULTS>
<TEST>
<BATCH>
<BATCH_OBJECTS><ORDER_NUM> 0 </ORDER_NUM></BATCH_OBJECTS>
</BATCH>
</TEST>
</ZAVACOR_RESULTS>
我希望结果看起来像这样 -
<ZAVACOR_RESULTS>
<TEST>
<BATCH>
<BATCH_OBJECTS><ORDER_NUM>0</ORDER_NUM></BATCH_OBJECTS>
</BATCH>
</TEST>
</ZAVACOR_RESULTS>
这是我的查询。任何帮助将不胜感激:
SELECT
XMLElement("ZAVACOR_RESULTS",
XMLAgg(
XMLElement("TEST",
XMLElement("BATCH", (case when t.batch is NULL then '<BATCH_OBJECTS> <ORDER_NUM> 0 </ORDER_NUM></BATCH_OBJECTS>' else t.batch end)),
(SELECT
XMLAgg(
XMLElement("BATCH_OBJECTS",
XMLElement("ORDER_NUMBER", (case when bo.ORDER_NUMBER is NULL then 0 else bo.ORDER_NUMBER end))
)
)
FROM BATCH_OBJECTS bo WHERE t.SAMPLE_NUMBER = bo.OBJECT_ID and
t.SAMPLE_NUMBER = 705000088556 and t.TEST_NUMBER = 4537048
)
)
)
).getClobVal() as XML
FROM TEST T WHERE t.SAMPLE_NUMBER = 705000088556 and t.test_number = 4537048;
数据关系:
TEST.TEST_NUMBER ---Number, Primary Key
TEST.BATCH --- Varchar2, Can be NULL
TEST.SAMPLE_NUMBER --- Number
BATCH_OBJECTS.BATCH --- Varchar2, Part of Composite Key
BATCH_OBJECTS.ORDER_NUMBER --- Number, Part of Composite Key
答案 0 :(得分:1)
您需要将固定字符串转换为自己的XML对象。您尚未显示表格结构。如果test.batch
是varchar2
(而不是XMLType
),您可以转换案例结果:
SELECT
XMLElement("ZAVACOR_RESULTS",
XMLAgg(
XMLElement("TEST",
XMLElement("BATCH", xmltype(case when t.batch is NULL then '<BATCH_OBJECTS> <ORDER_NUM> 0 </ORDER_NUM></BATCH_OBJECTS>' else t.batch end)),
...
如果test.batch
是XMLType
,那么只需转换字符串文字:
SELECT
XMLElement("ZAVACOR_RESULTS",
XMLAgg(
XMLElement("TEST",
XMLElement("BATCH", case when t.batch is NULL then xmltype('<BATCH_OBJECTS> <ORDER_NUM> 0 </ORDER_NUM></BATCH_OBJECTS>') else t.batch end),
...
这两种方式都给出了:
XML
-------------------------------------------------------
<ZAVACOR_RESULTS><TEST><BATCH><BATCH_OBJECTS>
<ORDER_NUM> 0 </ORDER_NUM>
</BATCH_OBJECTS>
</BATCH></TEST></ZAVACOR_RESULTS>
根据您对要尝试的内容的描述 - 使用示例数据和预期输出更容易理解 - 我认为您真正想要的是批处理对象表的外部联接,以及{{订单号的1}}如果没有一个:
NVL
SELECT XMLSerialize(CONTENT
XMLElement("ZAVACOR_RESULTS",
XMLElement("TEST",
XMLElement("BATCH",
XMLElement("BATCH_OBJECTS",
XMLAgg(
XMLElement("ORDER_NUMBER", nvl(bo.ORDER_NUMBER, 0))
)
)
)
)
) INDENT SIZE = 2
) as XML
FROM TEST T
LEFT JOIN BATCH_OBJECTS bo ON bo.BATCH = t.BATCH
WHERE t.SAMPLE_NUMBER = 705000088556
AND t.test_number = 4537048;
只是显示整齐;要传递到另一个应用程序,您可以坚持使用XMLSerialise
:
getClobVal
对于<ZAVACOR_RESULTS>
<TEST>
<BATCH>
<BATCH_OBJECTS>
<ORDER_NUMBER>0</ORDER_NUMBER>
</BATCH_OBJECTS>
</BATCH>
</TEST>
</ZAVACOR_RESULTS>
条记录的测试,提供:
batch_objects
如果要将多个测试组合到一个XML文档中,则可以使用另一个级别<ZAVACOR_RESULTS>
<TEST>
<BATCH>
<BATCH_OBJECTS>
<ORDER_NUMBER>12345</ORDER_NUMBER>
<ORDER_NUMBER>23456</ORDER_NUMBER>
</BATCH_OBJECTS>
</BATCH>
</TEST>
</ZAVACOR_RESULTS>
:
XMLAgg
我已经包含了一些属性来区分源行:
SELECT XMLSerialize(CONTENT
XMLElement("ZAVACOR_RESULTS",
XMLAgg(
XMLElement("TEST", XMLAttributes(t.TEST_NUMBER),
XMLElement("BATCH", XMLAttributes(t.BATCH),
XMLElement("BATCH_OBJECTS",
XMLAgg(
XMLElement("ORDER_NUMBER", nvl(bo.ORDER_NUMBER, 0))
)
)
)
)
)
) INDENT SIZE = 2
) as XML
FROM TEST T
LEFT JOIN BATCH_OBJECTS bo ON bo.BATCH = t.BATCH
GROUP BY t.TEST_NUMBER, t.BATCH;