0

是否可以在存储过程中使用带有 xquery 语句的逻辑 and 运算符来删除一个元素,前提是它的父元素具有特定值并且它具有特定值?如:

SET @profiles_xml = (SELECT profiles from tbl_applied_profiles WHERE profiles.value('(Profile/ID)[1]','int')= @profile_id)
    SET @profiles_xml.modify('
    delete
    if(/Profile/User/ID=sql:variable("@user_id")) and (/Profile/User/Activities/Activity/Name = sql:variable("@activity_name"))
    then (/Profile/User/Activities/Activity)
    else()
    ')

仅当用户 ID 元素和活动名称元素与给定参数匹配时,我才想删除活动。profile_xml 中的 xml 如下所示:

<Profile>
  <ID>20</ID>
  <User>
    <ID>BC4A18CA-AFB5-4268-BDA9-C990DAFE7783</ID>
    <Name>somename</Name>
    <Activities>
      <Activity>
         <Name>activity1</Name>
      </Activity>
    </Activities>
  </User>
</Profile>

但在未来,每个元素中可能会有多个元素,并且将多个元素作为元素的子元素。

4

1 回答 1

4
declare @XML xml = '
<Profile>
  <ID>20</ID>
  <User>
    <ID>BC4A18CA-AFB5-4268-BDA9-C990DAFE7783</ID>
    <Name>somename</Name>
    <Activities>
      <Activity>
         <Name>activity1</Name>
      </Activity>
    </Activities>
  </User>
</Profile>'

declare @user_id nvarchar(50) = '20'
declare @activity_name nvarchar(50) = 'activity1'

set @XML.modify('delete /Profile[ID = sql:variable("@user_id")]
                        /User/Activities/
                        Activity[Name = sql:variable("@activity_name")]') 

select @XML
于 2012-11-05T13:30:45.823 回答