在xquery insert的If条件中使用varchar存储过程参数

时间:2012-11-05 05:20:43

标签: sql xml sql-server-2008 tsql xquery

是否可以在存储过程中的xquery insert语句中使用nvarchar类型的sproc变量? 我的意思是这样的,只有当存在具有seek值的子元素时才会插入元素:

DECLARE @profiles_xml xml
DECLARE @profile_id int
DECLARE @user_id nvarchar(50)
SET @profile_id = 20
SET @user_id ='BC4A18CA-AFB5-4268-BDA9-C990DAFE7783'
SET @profiles_xml = (SELECT profiles from tbl_applied_profiles WHERE profiles.value('(Profile/ID)[1]','int')= @profile_id)
SET @profiles_xml.modify('
insert
if(/Profile/User/ID="user_id")
then <Activity>activity_name5</Activity>
else()
as first
into (/Profile/User/Activities)[1]')

@profiles_xml包含的xml如下所示:

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

1 个答案:

答案 0 :(得分:0)

您需要使用sql:variable("@user_id"),即

SET @profiles_xml.modify('
insert
if(/Profile/User/ID=sql:variable("@user_id"))
then <Activity>activity_name5</Activity>
else()
as first
into (/Profile/User/Activities)[1]');
select @profiles_xml;