• <tfoot id='UCJrS'></tfoot>
    <i id='UCJrS'><tr id='UCJrS'><dt id='UCJrS'><q id='UCJrS'><span id='UCJrS'><b id='UCJrS'><form id='UCJrS'><ins id='UCJrS'></ins><ul id='UCJrS'></ul><sub id='UCJrS'></sub></form><legend id='UCJrS'></legend><bdo id='UCJrS'><pre id='UCJrS'><center id='UCJrS'></center></pre></bdo></b><th id='UCJrS'></th></span></q></dt></tr></i><div id='UCJrS'><tfoot id='UCJrS'></tfoot><dl id='UCJrS'><fieldset id='UCJrS'></fieldset></dl></div>
      • <bdo id='UCJrS'></bdo><ul id='UCJrS'></ul>

      1. <small id='UCJrS'></small><noframes id='UCJrS'>

      2. <legend id='UCJrS'><style id='UCJrS'><dir id='UCJrS'><q id='UCJrS'></q></dir></style></legend>

        如何从 SQL Server 2008 返回 XML,该 XML 结构为多个选

        时间:2023-06-07
        <legend id='s2q6v'><style id='s2q6v'><dir id='s2q6v'><q id='s2q6v'></q></dir></style></legend>

          <i id='s2q6v'><tr id='s2q6v'><dt id='s2q6v'><q id='s2q6v'><span id='s2q6v'><b id='s2q6v'><form id='s2q6v'><ins id='s2q6v'></ins><ul id='s2q6v'></ul><sub id='s2q6v'></sub></form><legend id='s2q6v'></legend><bdo id='s2q6v'><pre id='s2q6v'><center id='s2q6v'></center></pre></bdo></b><th id='s2q6v'></th></span></q></dt></tr></i><div id='s2q6v'><tfoot id='s2q6v'></tfoot><dl id='s2q6v'><fieldset id='s2q6v'></fieldset></dl></div>

              <tfoot id='s2q6v'></tfoot>
                • <bdo id='s2q6v'></bdo><ul id='s2q6v'></ul>
                • <small id='s2q6v'></small><noframes id='s2q6v'>

                    <tbody id='s2q6v'></tbody>

                • 本文介绍了如何从 SQL Server 2008 返回 XML,该 XML 结构为多个选择共享一个公共父级的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  我尝试过使用FOR XML PATH"、FOR XML EXPLICIT"和FOR XML AUTO",但数据的结构从来没有使用正确的层次结构.

                  I've tried using "FOR XML PATH", "FOR XML EXPLICIT" and "FOR XML AUTO" but the data is never structured with the correct heirarchy.

                  基本上,我有一个父表(客户)和 3 个子表.每个表都有一个 customerid 列.从客户表到 3 个子表中的每一个都存在一对多关系.

                  Basically, I have one parent table (Customers) and 3 child tables. Each table has a customerid column. There is a one-to-many relationship from the Customers table to each of the 3 child tables.

                  作为一个模拟示例,我有一个父Customers"表,还有另外 3 个表 - Products、Hobbies 和 Vehicles - 所有表都通过 customerid 与 Customers 表相关.

                  As a mock example, I have a parent "Customers" table, and I have 3 other tables - Products, Hobbies and Vehicles - all related to the Customers table by a customerid.

                  实现以下结构的SQL代码是什么-

                  What is the SQL code to achieve the following kind of structure -

                  <Customers>
                      <Customer customerid="1" name="Fred">
                         <Products>
                             <Product productname="table" />
                             <Product productname="chair" />
                             <Product productname="wardrobe" />
                         </Products>
                         <Hobbies>
                             <Hobby hobbyname="Golf" />
                             <Hobby hobbyname="Swimming" />
                         </Hobbies>
                         <Vehicles>
                             <Vehicle name="Car" color="Red" />
                             <Vehicle name="Bicycle" color="Blue" />
                         </Vehicles>
                      </Customer>
                      <Customer customerid="2" name="Sue">
                         <Products>
                             <Product productname="CD player" />
                             <Product productname="Picture frame" />
                         </Products>
                         <Hobbies>
                             <Hobby hobbyname="Dancing" />
                             <Hobby hobbyname="Reading" />
                         </Hobbies>
                         <Vehicles>
                             <Vehicle name="Car" color="Yellow" />
                         </Vehicles>
                      </Customer>
                  </Customers>
                  

                  推荐答案

                  尝试类似的方法 - 它使用 FOR XML PATH 和 subselects 为给定客户创建链接"子节点(我将其限制为两个子表 - 但您应该了解它的要点"并能够将其扩展到任意数量的链接子表):

                  Try something like this - it uses FOR XML PATH and subselects to create the "linked" sub-nodes for a given customer (I limited this to two sub tables - but you should get the "gist" of it and be able to extend it to any number of linked subtables):

                  SELECT
                      CustomerID AS '@CustomerID',
                      CustName AS '@Name',
                  
                      (SELECT ProductName AS '@productname'
                       FROM dbo.Products p
                       WHERE p.CustomerID = c.CustomerID  
                       FOR XML PATH('Product'), TYPE) AS 'Products',
                  
                      (SELECT HobbyName AS '@hobbyname'
                       FROM dbo.Hobbies h 
                       WHERE h.CUstomerID = c.CustomerID
                       FOR XML PATH('Hobby'), TYPE) AS 'Hobbies'
                  FROM
                      dbo.Customers c
                  FOR XML PATH('Customer'), ROOT('Customers')
                  

                  给我一​​个类似的输出:

                  Gives me an output something like:

                  <Customers>
                    <Customer CustomerID="1" Name="Fred">
                      <Products>
                        <Product productname="Table" />
                        <Product productname="Wardrobe" />
                        <Product productname="Chair" />
                      </Products>
                      <Hobbies>
                        <Hobby hobbyname="Golf" />
                        <Hobby hobbyname="Swimming" />
                      </Hobbies>
                    </Customer>
                    <Customer CustomerID="2" Name="Sue">
                      <Products>
                        <Product productname="CD Player" />
                        <Product productname="Picture frame" />
                      </Products>
                      <Hobbies>
                        <Hobby hobbyname="Dancing" />
                        <Hobby hobbyname="Gardening" />
                        <Hobby hobbyname="Reading" />
                      </Hobbies>
                    </Customer>
                  </Customers>
                  

                  这篇关于如何从 SQL Server 2008 返回 XML,该 XML 结构为多个选择共享一个公共父级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:以递归方式将 xml 粉碎到数据库中 下一篇:在 SQL 中的 xml 列中搜索多个值

                  相关文章

                  最新文章

                  <small id='XdviF'></small><noframes id='XdviF'>

                  1. <legend id='XdviF'><style id='XdviF'><dir id='XdviF'><q id='XdviF'></q></dir></style></legend>

                    <tfoot id='XdviF'></tfoot>
                  2. <i id='XdviF'><tr id='XdviF'><dt id='XdviF'><q id='XdviF'><span id='XdviF'><b id='XdviF'><form id='XdviF'><ins id='XdviF'></ins><ul id='XdviF'></ul><sub id='XdviF'></sub></form><legend id='XdviF'></legend><bdo id='XdviF'><pre id='XdviF'><center id='XdviF'></center></pre></bdo></b><th id='XdviF'></th></span></q></dt></tr></i><div id='XdviF'><tfoot id='XdviF'></tfoot><dl id='XdviF'><fieldset id='XdviF'></fieldset></dl></div>
                      <bdo id='XdviF'></bdo><ul id='XdviF'></ul>