<legend id='5aFt3'><style id='5aFt3'><dir id='5aFt3'><q id='5aFt3'></q></dir></style></legend>
  • <small id='5aFt3'></small><noframes id='5aFt3'>

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

      1. 如何使用 for 循环使用 XQuery 删除 xml 节点

        时间:2023-06-07

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

                  <tbody id='HuEyg'></tbody>
              1. <small id='HuEyg'></small><noframes id='HuEyg'>

                  本文介绍了如何使用 for 循环使用 XQuery 删除 xml 节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  我在 SQL 存储过程中有一个 XML 参数 @Xml,如下所示:

                  I have an XML parameter @Xml in SQL Stored procedure like below:

                      <Root>
                     <Data>
                       <Id>1</Id>
                       <Name>Kevin</Name>
                       <Des>Des1</Des>
                     </Data>
                     <Data>
                       <Id>2</Id>
                       <Name>Alex</Name>
                       <Des>Des2</Des>
                     </Data>
                     <Data>
                       <Id>3</Id>
                       <Name>Amy</Name>
                       <Des>Des3</Des>
                     </Data>
                   </Root>
                  

                  现在,我想删除这个xml中的几个节点,过滤器是这样的(Id = 2 AND Name = 'Alex') OR (Id = 3 AND Name = 'Amy'),我不想使用 Cursor 或类似的东西,只需使用一个脚本来完成它,我正在尽我所能,但我无法得到答案,有人可以帮助我吗?提前致谢!!!

                  now, I want to delete several nodes in this xml, the filter is like this (Id = 2 AND Name = 'Alex') OR (Id = 3 AND Name = 'Amy'), I don't want to use Cursor or something like this, just using one single script to do it, I am try to my best for this, but I can't get the answer, anybody can help me ? Thanks in advance!!!

                  输出应该是:

                      <Root>
                     <Data>
                       <Id>1</Id>
                       <Name>Kevin</Name>
                       <Des>Des1</Des>
                     </Data>
                   </Root>
                  

                  PS:过滤器是一个#table,那么,实际的问题是,如何删除包含在#table中的XML中的特定记录?

                  PS: the filter is a #table, so, the actually question is , how to remove the specific records in XML which contains in #table?

                  Id    Name
                  2     Alex
                  3     Amy
                  

                  <小时>

                      Declare @Xml XML
                  set @Xml = '<Root>
                     <Data>
                       <Id>1</Id>
                       <Name>Kevin</Name>
                       <Des>Des1</Des>
                     </Data>
                     <Data>
                       <Id>2</Id>
                       <Name>Alex</Name>
                       <Des>Des2</Des>
                     </Data>
                     <Data>
                       <Id>3</Id>
                       <Name>Amy</Name>
                       <Des>Des3</Des>
                     </Data>
                   </Root>'
                  
                  create TABLE #tempResult 
                  (
                      Id Int,
                      Name Varchar(10)
                  )
                  
                  insert into #tempResult
                  values(2, 'Alex'), (3, 'Amy')
                  
                  SET @Xml = (
                  SELECT Node.value('Id[1]','int') AS PId, Node.value('Name[1]','Varchar(10)') AS PName 
                  FROM @Xml.nodes('//Data') AS T(Node)
                  OUTER APPLY (
                      SELECT tr.Id, tr.Name
                      FROM #tempResult tr
                      WHERE Node.value('Id[1]','int') = tr.Id and Node.value('Name[1]','Varchar(10)') = tr.Name
                  ) a
                  WHERE a.Id IS NULL
                  FOR XML PATH(''), TYPE
                  )
                  
                  select @Xml
                  drop table #tempResult
                  

                  我得到了这样的结果1凯文

                  I got results like this 1 Kevin

                  但我需要整个xml包含根节点:

                  But I need the the whole xml contains Root node:

                      <Root>
                    <Data>
                      <Id>1</Id>
                      <Name>Kevin</Name>
                      <Des>Des1</Des>
                    </Data>
                  </Root>
                  

                  我怎样才能达到这个目标?有什么帮助吗?我是 SQL 到 XML 的新手,请帮助我!!!

                  How can I reach this? Any help? I am an new leaner for SQL to XML, please help me !!!

                  推荐答案

                  我得到了答案,谢谢大家

                  I got the answer, thanks all you guys

                      declare @xml xml
                      set @xml = '<Root><Header>123</Header><Data><Id>1</Id><Name>Kevin</Name><Des>Des1</Des></Data><Data><Id>2</Id><Name>Alex</Name><Des>Des2</Des></Data><Data><Id>3</Id><Name>Amy</Name><Des>Des3</Des></Data><Tail>456</Tail></Root>'
                      --set @xml.modify('delete /Root/Data[(Id[.=1] and Name[.="Kevin"]) or (Id[.=2] and Name[.="Alex"])]')
                      select @xml
                      declare @parameter XML
                      set @parameter = (SELECT Node.value('(Id)[1]', 'Int') AS Id,
                                              Node.value('(Name)[1]', 'Varchar(10)') AS Name
                                      FROM @xml.nodes('Root/Data') TempXML(Node)
                                      WHERE Node.value('(Id)[1]', 'Int') != 3
                                      for xml path('Root'), TYPE)
                      --select @parameter
                      declare @sql nvarchar(max)
                      set @sql = convert(nvarchar(max), 
                       @parameter.query('for $i in (/Root) return concat("(Id[.=", 
                                                                              string($i/Id[1]),
                                                                              "] and Name[.=""", 
                                                                              string($i/Name[1]),
                                                                              """]) or")')
                                                                              )
                      set @sql = '['+substring(@sql,0,len(@sql)-2)+']'
                      set @sql = 'set @xml.modify(''delete /Root/Data'+@sql+''')'
                      select @sql
                      exec sp_executesql @sql, N'@xml xml output', @xml output
                      select @xml
                  

                  这篇关于如何使用 for 循环使用 XQuery 删除 xml 节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:使用 SQL 在 XML 中插入节点 下一篇:SQL Server 中的 XML 解析

                  相关文章

                  最新文章

                    <bdo id='vTB1D'></bdo><ul id='vTB1D'></ul>
                  1. <tfoot id='vTB1D'></tfoot>

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

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