【发布时间】:2020-05-22 14:31:50
【问题描述】:
我一直坚持使用 T-SQl 查询创建 XML 数据:
这里是查询:
(Select case when pc.CASE_TYPE_ID = 4 then prt.DESIGNATED_COUNTRYS end as "countries"
for xml path ('countries'), type )
,(Select case when pc.CASE_TYPE_ID = 4 then prt.classes end as classes
for xml path ('countries'), type )
from PAT_REPORT_TABLE prt join pat_case pc on pc.case_id = prt.case_id
for XML PATH('ip_row'),type, ROOT ('ip_data')
结果如下:
<ip_data>
<ip_row>
<countries>
<countries>AT, BX, CH, CS, DE, EG, FI, FR, HU, IT, MA, RO</countries>
</countries>
<classes>
<classes>01, 04</classes>
</classes>
</ip_row>
</ip_data>
但我需要单独的行中的国家和类别。 所以我想要的结果应该是:
<ip_data>
<ip_row>
<countries>
<country>AT</country>
<country>BX</country>
<country>CH</country>
<country>CS</country>
<country>DE</country>
<country>EG</country>
<country>FI</country>
<country>FR</country>
<country>HU</country>
</countries>
<classes>
<class>01</class>
<class>04</class>
</classes>
</ip_row>
</ip_data>
我该如何拆分?
【问题讨论】:
-
DESIGNATED_COUNTRYS列的数据类型和内容是什么?请尝试设置minimal reproducible example(*带有 DDL 和 INSERT 的独立解决方案,至少有一些示例数据和预期输出)。 -
正如@Shnugo 指出的那样,请提供以下内容:(1)DDL 和样本数据填充,即 CREATE 表加上 INSERT T-SQL 语句。 (2) 你需要做什么,即逻辑,你的代码尝试实现它。 (3) 基于上述#1 中的样本数据的期望输出。 (4) 你的 SQL Server 版本 (SELECT @@version;)
标签: sql-server xml tsql split