SQL XML 嵌套查询

发布于 2025-01-03 01:22:49 字数 1158 浏览 0 评论 0原文

如何根据这样的查询生成下面类似的XML?

SELECT
    t1.Code,    
    (SELECT
        CASE 
            WHEN t1.Type = 5 THEN
                (SELECT top 1
                    t2.CodeName
                FROM dbo.Table2 t2
                WHERE t2.Id = t4.Id
          FOR XML  PATH('Code'), TYPE)          
      ELSE  
          (SELECT 
             t3.CodeName
          FROM dbo.Table3 t3
                WHERE t3.Id = t4.Id
                FOR XML PATH('Code'), TYPE)                     
      END
    FROM dbo.Table4 t4
    WHERE t4.Id = t1.Id
    FOR XML PATH('CodeNames'), TYPE)    
FROM dbo.Table1  t1 WITH (NOLOCK)
FOR XML RAW ('Table1Child'), TYPE, ROOT('Table1Root')

预期产出

<Table1Root>
  <Table1Child Code="ABC">
    <CodeNames>
      <Code Name="A1" />
      <Code Name="B2" />
      <Code Name="C3" />
    </CodeNames>
  </Table1Child>
  <Table1Child Code="DEF">
    <CodeNames>
      <Code Name="D4" />
      <Code Name="E5" />
      <Code Name="F6" />
    </CodeNames>
  </Table1Child>
</Table1Root>

How to generate below like XML based on such query?

SELECT
    t1.Code,    
    (SELECT
        CASE 
            WHEN t1.Type = 5 THEN
                (SELECT top 1
                    t2.CodeName
                FROM dbo.Table2 t2
                WHERE t2.Id = t4.Id
          FOR XML  PATH('Code'), TYPE)          
      ELSE  
          (SELECT 
             t3.CodeName
          FROM dbo.Table3 t3
                WHERE t3.Id = t4.Id
                FOR XML PATH('Code'), TYPE)                     
      END
    FROM dbo.Table4 t4
    WHERE t4.Id = t1.Id
    FOR XML PATH('CodeNames'), TYPE)    
FROM dbo.Table1  t1 WITH (NOLOCK)
FOR XML RAW ('Table1Child'), TYPE, ROOT('Table1Root')

Expected output

<Table1Root>
  <Table1Child Code="ABC">
    <CodeNames>
      <Code Name="A1" />
      <Code Name="B2" />
      <Code Name="C3" />
    </CodeNames>
  </Table1Child>
  <Table1Child Code="DEF">
    <CodeNames>
      <Code Name="D4" />
      <Code Name="E5" />
      <Code Name="F6" />
    </CodeNames>
  </Table1Child>
</Table1Root>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

岁月打碎记忆 2025-01-10 01:22:49

使用 UNION 而不是 CASE 并使用 @ 创建属性。

SELECT
    t1.Code,    
    (SELECT
      (SELECT CodeName as '@Name'
       FROM (
             SELECT t2.CodeName
             FROM Table2 t2
             WHERE t2.Id = t4.Id AND
                   t1.Type = 5
             UNION ALL
             SELECT t3.CodeName
             FROM Table3 t3
             WHERE t3.Id = t4.Id AND
                   t1.Type <> 5
            ) AS T 
       FOR XML PATH('Code'), TYPE)          
     FROM Table4 t4
     WHERE t4.Id = t1.Id
     FOR XML PATH('CodeNames'), TYPE)    
FROM Table1 t1
FOR XML RAW ('Table1Child'), TYPE, ROOT('Table1Root')

看来您实际上不需要使用 Table4

SELECT
    t1.Code,    
      (SELECT CodeName as '@Name'
       FROM (
             SELECT t2.CodeName
             FROM Table2 t2
             WHERE t2.Id = t1.Id AND
                   t1.Type = 5
             UNION ALL
             SELECT t3.CodeName
             FROM Table3 t3
             WHERE t3.Id = t1.Id AND
                   t1.Type <> 5
            ) AS T 
       FOR XML PATH('Code'), ROOT('CodeNames'), TYPE)          
FROM Table1 t1
FOR XML RAW ('Table1Child'), TYPE, ROOT('Table1Root')

Do a UNION instead of CASE and use @ to create an attribute.

SELECT
    t1.Code,    
    (SELECT
      (SELECT CodeName as '@Name'
       FROM (
             SELECT t2.CodeName
             FROM Table2 t2
             WHERE t2.Id = t4.Id AND
                   t1.Type = 5
             UNION ALL
             SELECT t3.CodeName
             FROM Table3 t3
             WHERE t3.Id = t4.Id AND
                   t1.Type <> 5
            ) AS T 
       FOR XML PATH('Code'), TYPE)          
     FROM Table4 t4
     WHERE t4.Id = t1.Id
     FOR XML PATH('CodeNames'), TYPE)    
FROM Table1 t1
FOR XML RAW ('Table1Child'), TYPE, ROOT('Table1Root')

It looks like you actually don't need to use Table4.

SELECT
    t1.Code,    
      (SELECT CodeName as '@Name'
       FROM (
             SELECT t2.CodeName
             FROM Table2 t2
             WHERE t2.Id = t1.Id AND
                   t1.Type = 5
             UNION ALL
             SELECT t3.CodeName
             FROM Table3 t3
             WHERE t3.Id = t1.Id AND
                   t1.Type <> 5
            ) AS T 
       FOR XML PATH('Code'), ROOT('CodeNames'), TYPE)          
FROM Table1 t1
FOR XML RAW ('Table1Child'), TYPE, ROOT('Table1Root')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文