<bdo id='UwVa5'></bdo><ul id='UwVa5'></ul>

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

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

        SQL Server 将 XML 子节点附加到父节点

        时间:2023-08-20
        <legend id='VOXqZ'><style id='VOXqZ'><dir id='VOXqZ'><q id='VOXqZ'></q></dir></style></legend>
          <i id='VOXqZ'><tr id='VOXqZ'><dt id='VOXqZ'><q id='VOXqZ'><span id='VOXqZ'><b id='VOXqZ'><form id='VOXqZ'><ins id='VOXqZ'></ins><ul id='VOXqZ'></ul><sub id='VOXqZ'></sub></form><legend id='VOXqZ'></legend><bdo id='VOXqZ'><pre id='VOXqZ'><center id='VOXqZ'></center></pre></bdo></b><th id='VOXqZ'></th></span></q></dt></tr></i><div id='VOXqZ'><tfoot id='VOXqZ'></tfoot><dl id='VOXqZ'><fieldset id='VOXqZ'></fieldset></dl></div>

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

              <tbody id='VOXqZ'></tbody>

                <bdo id='VOXqZ'></bdo><ul id='VOXqZ'></ul>

                1. <tfoot id='VOXqZ'></tfoot>
                2. 本文介绍了SQL Server 将 XML 子节点附加到父节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我需要一个脚本来将新的 xml 子节点插入/附加到预先存在的 xml 父节点.

                  I need to have a script which can insert / append new xml child nodes to a pre-existing xml parent node.

                  --New child nodes
                  DECLARE @XMLChildData XML
                  SET @XMLChildData = '
                  <Persons>
                      <Person>
                          <Firstname>Gary</Firstname>
                          <Surname>Smith</Surname>
                          <Telephone>0115547899</Telephone>
                          <Address>
                              <AddressLine>1 Church Lane</AddressLine>
                              <AddressLine>Rosebank</AddressLine>
                              <AddressLine>Houghton</AddressLine>
                              <AddressLine>South Africa</AddressLine>
                          </Address>
                      </Person>
                      <Person>
                          <Firstname>Wayne</Firstname>
                          <Surname>Farmey</Surname>
                          <Telephone>0117453269</Telephone>
                          <Address>
                              <AddressLine>51 Oak Street</AddressLine>
                              <AddressLine>Rivionia</AddressLine>
                              <AddressLine>Sandton</AddressLine>
                              <AddressLine>South Africa</AddressLine>
                          </Address>
                      </Person>
                      <Person>
                          <Firstname>Mark</Firstname>
                          <Surname>Jones</Surname>
                          <Telephone>0119854741</Telephone>
                          <Address>
                              <AddressLine>4 Arum Lane</AddressLine>
                              <AddressLine>Glen Hazel</AddressLine>
                              <AddressLine>Johannesburg</AddressLine>
                              <AddressLine>South Africa</AddressLine>
                          </Address>
                      </Person>
                  </Persons>'
                  
                  --Existing parent node
                  DECLARE @XMLParentData XML
                  SET @XMLParentData = '
                  <Persons>
                      <Person>
                          <Firstname>Sarah</Firstname>
                          <Surname>Gray</Surname>
                          <Telephone>0113265874</Telephone>
                          <Address>
                              <AddressLine>78 Emerl Aveune</AddressLine>
                              <AddressLine>Fourways</AddressLine>
                              <AddressLine>Sandton</AddressLine>
                              <AddressLine>South Africa</AddressLine>
                          </Address>
                      </Person>
                      <Person>
                          <Firstname>Jenna</Firstname>
                          <Surname>Reed</Surname>
                          <Telephone>0114781102</Telephone>
                          <Address>
                              <AddressLine>6 Park Lane</AddressLine>
                              <AddressLine>Parkhurst</AddressLine>
                              <AddressLine>Rosebank</AddressLine>
                              <AddressLine>South Africa</AddressLine>
                          </Address>
                      </Person>
                      <Person>
                          <Firstname>Mike</Firstname>
                          <Surname>Wilke</Surname>
                          <Telephone>0116532003</Telephone>
                          <Address>
                              <AddressLine>22 High Road</AddressLine>
                              <AddressLine>Modderfontein</AddressLine>
                              <AddressLine>Edenvale</AddressLine>
                              <AddressLine>South Africa</AddressLine>
                          </Address>
                      </Person>
                  </Persons>'
                  

                  我希望最终结果是:

                  <Persons>
                      <Person>
                          <Firstname>Sarah</Firstname>
                          <Surname>Gray</Surname>
                          <Telephone>0113265874</Telephone>
                          <Address>
                              <AddressLine>78 Emerl Aveune</AddressLine>
                              <AddressLine>Fourways</AddressLine>
                              <AddressLine>Sandton</AddressLine>
                              <AddressLine>South Africa</AddressLine>
                          </Address>
                      </Person>
                      <Person>
                          <Firstname>Jenna</Firstname>
                          <Surname>Reed</Surname>
                          <Telephone>0114781102</Telephone>
                          <Address>
                              <AddressLine>6 Park Lane</AddressLine>
                              <AddressLine>Parkhurst</AddressLine>
                              <AddressLine>Rosebank</AddressLine>
                              <AddressLine>South Africa</AddressLine>
                          </Address>
                      </Person>
                      <Person>
                          <Firstname>Mike</Firstname>
                          <Surname>Wilke</Surname>
                          <Telephone>0116532003</Telephone>
                          <Address>
                              <AddressLine>22 High Road</AddressLine>
                              <AddressLine>Modderfontein</AddressLine>
                              <AddressLine>Edenvale</AddressLine>
                              <AddressLine>South Africa</AddressLine>
                          </Address>
                      </Person>
                      <Person>
                          <Firstname>Gary</Firstname>
                          <Surname>Smith</Surname>
                          <Telephone>0115547899</Telephone>
                          <Address>
                              <AddressLine>1 Church Lane</AddressLine>
                              <AddressLine>Rosebank</AddressLine>
                              <AddressLine>Houghton</AddressLine>
                              <AddressLine>South Africa</AddressLine>
                          </Address>
                      </Person>
                      <Person>
                          <Firstname>Wayne</Firstname>
                          <Surname>Farmey</Surname>
                          <Telephone>0117453269</Telephone>
                          <Address>
                              <AddressLine>51 Oak Street</AddressLine>
                              <AddressLine>Rivionia</AddressLine>
                              <AddressLine>Sandton</AddressLine>
                              <AddressLine>South Africa</AddressLine>
                          </Address>
                      </Person>
                      <Person>
                          <Firstname>Mark</Firstname>
                          <Surname>Jones</Surname>
                          <Telephone>0119854741</Telephone>
                          <Address>
                              <AddressLine>4 Arum Lane</AddressLine>
                              <AddressLine>Glen Hazel</AddressLine>
                              <AddressLine>Johannesburg</AddressLine>
                              <AddressLine>South Africa</AddressLine>
                          </Address>
                      </Person>
                  </Persons>
                  

                  我知道我需要使用 .modify(),但是我不确定如何遍历子节点并将每个子节点 "" 插入/附加到父节点中"" 节点.

                  I know i need to use the .modify(), however i am not sure how to iterate through the child nodes and insert / append each child "<person>" node into the parent "<persons>" node.

                  我认为它需要类似于以下内容

                  I would think it would need to be something similiar as below

                  SET @XMLParentData.modify('
                      insert     
                          (
                              sql:variable("@XMLChildData")
                          )
                      after
                          (/Person[1]/Person[1])
                  ')
                  
                  SELECT @XMLData
                  

                  推荐答案

                  从@XMLChildData 中提取 Person 节点到一个单独的变量,并将其添加到 Persons 节点@XMLParentData.

                  Extract the Person nodes from @XMLChildData to a separate variable and add that to the Persons node of @XMLParentData.

                  DECLARE @PersonList XML
                  
                  SET @PersonList = @XMLChildData.query('Persons/*')
                  
                  SET @XMLParentData.modify('insert sql:variable("@PersonList") as last into /Persons[1]')
                  
                  SELECT @XMLParentData
                  

                  另一种方法是从两个变量中提取 Person 节点并使用 FOR XML PATH 重建 Persons 节点.

                  Another way is to extract the Person nodes from both variables and rebuild the Persons node using FOR XML PATH.

                  SET @XMLParentData = (
                                       SELECT @XMLParentData.query('/Persons/Person'),
                                              @XMLChildData.query('/Persons/Person')
                                       FOR XML PATH(''), ROOT('Persons'), TYPE
                                       )
                  

                  这篇关于SQL Server 将 XML 子节点附加到父节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:如何在 ORACLE 中使用 SQL UPDATE 命令将 BLOB 数据附加 下一篇:将从不同查询返回的行附加到一个

                  相关文章

                  最新文章

                    <bdo id='pcvb8'></bdo><ul id='pcvb8'></ul>
                  <tfoot id='pcvb8'></tfoot>

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

                  <legend id='pcvb8'><style id='pcvb8'><dir id='pcvb8'><q id='pcvb8'></q></dir></style></legend>

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