FOR XML 表达式的嵌套

发布于 2024-10-17 21:41:34 字数 1401 浏览 4 评论 0原文

我尝试了类似于 此处显示的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浮华 2024-10-24 21:41:34

使用此查询:

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,
       Null,
       Steps.Name,
       NULL
FROM Steps
JOIN StepTrees ON Steps.StepTreeId = StepTrees.Id
WHERE StepTrees.Name = 'ALFKI' 
UNION ALL
SELECT DISTINCT 3,
       2,
       Null,
       Steps.Name,
       Results.id
FROM StepTrees
JOIN Steps ON Steps.StepTreeId = StepTrees.Id
JOIN Results ON Steps.StepId = Results.StepId
ORDER BY [Step!2!Name],[Result!3!id]
FOR XML EXPLICIT

Use this 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,
       Null,
       Steps.Name,
       NULL
FROM Steps
JOIN StepTrees ON Steps.StepTreeId = StepTrees.Id
WHERE StepTrees.Name = 'ALFKI' 
UNION ALL
SELECT DISTINCT 3,
       2,
       Null,
       Steps.Name,
       Results.id
FROM StepTrees
JOIN Steps ON Steps.StepTreeId = StepTrees.Id
JOIN Results ON Steps.StepId = Results.StepId
ORDER BY [Step!2!Name],[Result!3!id]
FOR XML EXPLICIT
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文