問題描述
我在 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模板網!