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

2021-10-01 00:00:00 xml sql xquery sql-server

我在 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

相关文章