使用存储过程删除XML块

时间:2015-05-01 19:32:19

标签: sql sql-server xml

我们假设我有一个名为 Config 的表,其中包含一个XML列,其中包含XML数据。

SELECT * FROM Config

返回:

<ArrayOfKeyValueOfstringstring xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
  <KeyValueOfstringstring>
    <Key>402297</Key>
    <Value>1f98e48c0302ae2c1dfc3c2a781df2fb</Value>
  </KeyValueOfstringstring>
  <KeyValueOfstringstring>
    <Key>403121</Key>
    <Value>6784bf087329f2bef51c0b61753d22c3</Value>
  </KeyValueOfstringstring>
  <KeyValueOfstringstring>
    <Key>403131</Key>
    <Value>3711133c97ccca28c3a89ef1bd424fdf</Value>
  </KeyValueOfstringstring>
<ArrayOfKeyValueOfstringstring>

我想编写一个名为 DeleteXMLByKey 的存储过程,它接受一个整数,并删除密钥所在的XML块。

所以,如果我写:

EXEC DeleteXMLByKey 402297

我期待当我从表格中选择*时,我得到:

<ArrayOfKeyValueOfstringstring xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
  <KeyValueOfstringstring>
    <Key>403121</Key>
    <Value>6784bf087329f2bef51c0b61753d22c3</Value>
  </KeyValueOfstringstring>
  <KeyValueOfstringstring>
    <Key>403131</Key>
    <Value>3711133c97ccca28c3a89ef1bd424fdf</Value>
  </KeyValueOfstringstring>
<ArrayOfKeyValueOfstringstring>

(此XML不再具有402297块)

有什么办法可以通过SQL做到这一点吗?

1 个答案:

答案 0 :(得分:1)

这应该有效:

SELECT  
       Tbl.Col.value('Key[1]', 'int') [Key],  
       Tbl.Col.value('Value[1]', 'varchar(100)') [Value]
FROM   @xml.nodes('//ArrayOfKeyValueOfstringstring[1]/KeyValueOfstringstring') Tbl(Col)
where Tbl.Col.value('Key[1]', 'int') <> 402297
for xml path ('KeyValueOfstringstring'), Root('ArrayOfKeyValueOfstringstring')

我必须改变你的XML才能让它发挥作用。 ArrayOfKeyValueOfstringstring标记未终止。

编辑: 在您的特定情况下,您需要更新表中的所有xml列,因此您的代码可能如下所示:

--declare variable with xml
declare @xml as xml
set @xml =
'<ArrayOfKeyValueOfstringstring> xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.microsoft.com/2003/10/Serialization/Arrays"
  <KeyValueOfstringstring>
    <Key>402297</Key>
    <Value>1f98e48c0302ae2c1dfc3c2a781df2fb</Value>
  </KeyValueOfstringstring>
  <KeyValueOfstringstring>
    <Key>403121</Key>
    <Value>6784bf087329f2bef51c0b61753d22c3</Value>
  </KeyValueOfstringstring>
  <KeyValueOfstringstring>
    <Key>403131</Key>
    <Value>3711133c97ccca28c3a89ef1bd424fdf</Value>
  </KeyValueOfstringstring>
</ArrayOfKeyValueOfstringstring>
'

--create temp table with xml field
select @xml xmlfield
into #xml

--update the xml field to remove key 4002297
update #xml
set xmlfield =convert
    (xml,
    (SELECT  
           Tbl.Col.value('Key[1]', 'int') [Key],  
           Tbl.Col.value('Value[1]', 'varchar(100)') [Value]
    FROM   xmlfield.nodes('//ArrayOfKeyValueOfstringstring[1]/KeyValueOfstringstring') Tbl(Col)
    where Tbl.Col.value('Key[1]', 'int') <> 402297
    for xml path ('KeyValueOfstringstring'), Root('ArrayOfKeyValueOfstringstring'), Type
    ))
from #xml

--View Results
select * from #xml

结果:

<ArrayOfKeyValueOfstringstring>
  <KeyValueOfstringstring>
    <Key>403121</Key>
    <Value>6784bf087329f2bef51c0b61753d22c3</Value>
  </KeyValueOfstringstring>
  <KeyValueOfstringstring>
    <Key>403131</Key>
    <Value>3711133c97ccca28c3a89ef1bd424fdf</Value>
  </KeyValueOfstringstring>
</ArrayOfKeyValueOfstringstring>