FOR XML 表达式的嵌套
我尝试了类似于 此处显示的 FOR XML 查询 在副标题“FOR XML 表达式的嵌套”下。我的问题是我必须具有三个 XML 元素级别。结果应该是这样的。
<StepTree Name="ALFKI">
<Step Name="Foo">
<Result id="123" />
<Result id="456" />
</Step>
<Step Name="Bar">
<Result id="789" />
<Result id="987" />
</Step>
</StepTree >
我尝试过这种类型的查询。
SELECT 1 as TAG,
NULL as Parent,
StepTrees.Name AS [StepTree!1!Name],
NULL as [Step!2!Name],
NULL as [Result!3!id]
FROM StepTrees
WHERE StepTrees.Name = 'ALFKI'
UNION ALL
SELECT 2,
1,
StepTrees.Name,
Steps.Name,
NULL
FROM Steps
JOIN StepTrees ON Steps.StepTreeId = StepTrees.Id
WHERE StepTrees.Name = 'ALFKI'
UNION ALL
SELECT DISTINCT 3,
2,
StepTrees.Name,
Steps.Name,
Results.id
FROM StepTrees
JOIN Steps ON Steps.StepTreeId = StepTrees.Id
JOIN Results ON Steps.StepId = Results.StepId
FOR XML EXPLICIT
生成的 XML 如下所示。
<StepTree Name="ALFKI">
<Step Name="Foo" />
<Step Name="Bar">
<Result id="123" />
<Result id="456" />
<Result id="789" />
<Result id="987" />
</Step>
</StepTree >
有什么想法吗?
I tried a FOR XML query similar to what's shown here under the subtitle 'Nesting of FOR XML Expressions'. My problem is that I have to have three XML element levels. The results should be something like this.
<StepTree Name="ALFKI">
<Step Name="Foo">
<Result id="123" />
<Result id="456" />
</Step>
<Step Name="Bar">
<Result id="789" />
<Result id="987" />
</Step>
</StepTree >
I tried this type of query.
SELECT 1 as TAG,
NULL as Parent,
StepTrees.Name AS [StepTree!1!Name],
NULL as [Step!2!Name],
NULL as [Result!3!id]
FROM StepTrees
WHERE StepTrees.Name = 'ALFKI'
UNION ALL
SELECT 2,
1,
StepTrees.Name,
Steps.Name,
NULL
FROM Steps
JOIN StepTrees ON Steps.StepTreeId = StepTrees.Id
WHERE StepTrees.Name = 'ALFKI'
UNION ALL
SELECT DISTINCT 3,
2,
StepTrees.Name,
Steps.Name,
Results.id
FROM StepTrees
JOIN Steps ON Steps.StepTreeId = StepTrees.Id
JOIN Results ON Steps.StepId = Results.StepId
FOR XML EXPLICIT
The resulting XML is as follows.
<StepTree Name="ALFKI">
<Step Name="Foo" />
<Step Name="Bar">
<Result id="123" />
<Result id="456" />
<Result id="789" />
<Result id="987" />
</Step>
</StepTree >
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用此查询:
Use this query: