根据元素值从SQL Server 2008表列中删除xml节点

时间:2012-09-06 11:24:01

标签: sql-server-2008 xquery element

我有一个xml结构如下

<ControlVisibilityRule xmlns="urn:gjensidige:processguide:201201">
   <Id>e68c53a3-79ba-41e4-81cb-341050016783</Id>
   <Code>b53b687c-2617-4d02-8aa0-4d0d0898bd15_Code</Code>  
   <Author />  
   <Updated>9/5/2012</Updated>  
   <Sequence>0</Sequence>  
   <FromControls>    
       <Control>      
            <Code>ada</Code>      
            <Id>ba2abc55-3b25-4280-8bd0-a1d23a4575d0</Id>      
            <FilterValues>        
                <FilterValue xmlns:p5="urn:gjensidige:processguide:201201" p5:Id="e2b830f8-9a58-4edf-b56c-4c5a9580f362" p5:Code="1" p5:LookupId="ebb6066f-a976-4dcb-aabe-07c4f7d2686b" />
                <FilterValue xmlns:p5="urn:gjensidige:processguide:201201" p5:Id="44e268ef-2869-4df1-b61e-c59c2d3f1a5a" p5:Code="56" p5:LookupId="ebb6066f-a976-4dcb-aabe-07c4f7d2686b" />
            </FilterValues>    
       </Control>  
   </FromControls>  
   <ToControls>    
       <Control>      
           <Code>adeUnittest01</Code>      
           <Id>0a1cd240-20ee-4405-9613-d3006693c390</Id>    
       </Control>  
   </ToControls>  
   <IsVisible>True</IsVisible>
</ControlVisibilityRule>

我想删除xpath中的元素Id

/qn:ControlVisibilityRule/qn:ToControls/qn:Control/qn:Id  

,其值为0a1cd240-20ee-4405-9613-d3006693c390。我已经完成了直到下面,但我确定我错过了元素值与变量的比较。

 declare @Id nvarchar(50)
 declare @ruleId nvarchar(50) 

 ;WITH XMLNAMESPACES ('urn:gjensidige:processguide:201201' as qn)
 update 
     pdr_processdefinitionrule
 set 
     PDR_RuleXml.modify('delete (/qn:ControlVisibilityRule/qn:ToControls/qn:Control/qn:Id=sql:variable("@Id"))')
 where pdr_guid = @ruleId   

任何指导将不胜感激

1 个答案:

答案 0 :(得分:0)

你需要这样做:

 ;WITH XMLNAMESPACES ('urn:gjensidige:processguide:201201' as qn)
 update 
     pdr_processdefinitionrule
 set 
     PDR_RuleXml.modify('delete (/qn:ControlVisibilityRule/qn:ToControls/qn:Control/qn:Id[text()=sql:variable("@Id")])')
 where 
     pdr_guid = @ruleId   

基本上,您要删除qn:Id元素,其中text()(XML元素包含的文本)与变量@Id匹配。