对此的解决方案正在逃避我,我希望你能提供帮助。
表 [restrictions] 中的数据集如下所示
item_uid include the_value
00035061 FALSE 'AZ'
00035061 FALSE 'CA'
00035061 TRUE 'NC'
00035061 TRUE 'SC'
预期的 XML 结果应如下所示:
<item>
<item_uid>00035061</item_uid>
<restrictions>
<inclusions>
<include>NC</include>
<include>SC</include>
</inclusions>
<exclusions>
<exclude>AZ</exclude>
<exclude>CA</exclude>
</exclusions>
</restrictions>
<item>
我可以分别组装每一面;
SELECT the_value AS 'data()'
FROM restrictions
WHERE include = TRUE AND item_uid = '00031762'
FOR XML PATH ('include'), ROOT ('inclusions')
接着
SELECT the_value AS 'data()'
FROM restrictions
WHERE include = FALSE AND item_uid = '00031762'
FOR XML PATH ('exclude'), ROOT ('exclusions')
但无法弄清楚如何在一条 SQL 语句中生成此 XML。感谢你的协助!