T-SQL从XML中删除节点

时间:2014-09-15 19:05:13

标签: sql sql-server xml

我需要帮助,删除使用Sql server 2012的TabID ="声明" m的错误节点

<ClaimErrors ClaimID="73">
  <Error ErrorMessage="offer timeframe" TabID="Offer" FieldName="PostMarkDate" ErrorType="FrontEnd" NoteID="2" />
  <Error ErrorMessage=" code is not valid" TabID="Claim" FieldName="None" NoteID="3811257" ErrorType="BackEnd Error" />
</ClaimErrors>



declare @XML table (data xml)
INsert into @XML select '<ClaimErrors ClaimID="73"><Error ErrorMessage="offer timeframe" TabID="Offer" FieldName="PostMarkDate" ErrorType="FrontEnd" NoteID="2" />
<Error ErrorMessage="code is not valid" TabID="Claim" FieldName="None" NoteID="3811257" ErrorType="BackEnd" /></ClaimErrors>'
update @XML set  data.modify('delete //ClaimErrors/Error[text()][contains(.,"TabID=Claim")]')

我正在使用data.modify(&#39;删除// ClaimErrors /错误/但没有希望。

任何帮助将不胜感激.. 感谢

1 个答案:

答案 0 :(得分:1)

declare @XML table (data xml)
INsert into @XML select '<ClaimErrors ClaimID="73"><Error ErrorMessage="offer timeframe" TabID="Offer" FieldName="PostMarkDate" ErrorType="FrontEnd" NoteID="2" />
<Error ErrorMessage="code is not valid" TabID="Claim" FieldName="None" NoteID="3811257" ErrorType="BackEnd" /></ClaimErrors>'
declare @var nvarchar(256)= 'Claim'
update @XML set  data.modify('delete (/ClaimErrors/Error[@TabID=sql:variable("@var")])[1]')
select data from @XML