使用varchar变量中的值更新xml类型的SQL变量

使用varchar变量中的值更新xml类型的SQL变量,sql,sql-server,xml,Sql,Sql Server,Xml,我有一个xml作为 declare @EventBodyXml xml set @EventBodyXml =' <LocationCopied xmlns="http://schemas.datacontract.org/2004/07/someName" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"> <Location xmlns:a="http://

我有一个xml作为

declare @EventBodyXml xml
set @EventBodyXml ='
                <LocationCopied xmlns="http://schemas.datacontract.org/2004/07/someName" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
                <Location xmlns:a="http://schemas.datacontract.org/2004/07/someAnotherName">
                <a:Name>SomeLocationName</a:Name></Location>
                </LocationCopied>'
我试过下面这样的方法

    set @EventBodyXml.modify('replace value of (
                declare default element namespace "http://schemas.datacontract.org/2004/07/SCS.Domain.BusinessObjectManagement.Contract.EventModel";
                declare namespace a="http://schemas.datacontract.org/2004/07/SCS.Domain.BusinessObjectManagement.Contract.ViewModel";
                /LocationCopied/Location/a:Name/text() ) with  sql:variable("@uName") ') ;
但我得到的错误就像

Msg 2205,第16级,状态1,第27行XQuery[modify()]:“”“被删除 预料之中


您只是错过了
REPLACE

declare @EventBodyXml xml
set @EventBodyXml ='
                <LocationCopied xmlns="http://schemas.datacontract.org/2004/07/someName" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
                <Location xmlns:a="http://schemas.datacontract.org/2004/07/someAnotherName">
                <a:Name>SomeLocationName</a:Name></Location>
                </LocationCopied>'


declare @uName nvarchar(100)
set @uName=@EventBodyXml.query(
'declare default element namespace  "http://schemas.datacontract.org/2004/07/someName";
declare namespace a="http://schemas.datacontract.org/2004/07/someAnotherName";
/LocationCopied/Location/a:Name').value('.', 'nvarchar(100)') ;

select @uName

set @EventBodyXml.modify('
declare default element namespace "http://schemas.datacontract.org/2004/07/someName";
declare namespace a="http://schemas.datacontract.org/2004/07/someAnotherName";
replace value of (/LocationCopied/Location/a:Name/text())[1]
with sql:variable("@uName")
');

print cast(@EventBodyXml as nvarchar(max))

@Roshan很高兴这对你有帮助:)
declare @EventBodyXml xml
set @EventBodyXml ='
                <LocationCopied xmlns="http://schemas.datacontract.org/2004/07/someName" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
                <Location xmlns:a="http://schemas.datacontract.org/2004/07/someAnotherName">
                <a:Name>SomeLocationName</a:Name></Location>
                </LocationCopied>'


declare @uName nvarchar(100)
set @uName=@EventBodyXml.query(
'declare default element namespace  "http://schemas.datacontract.org/2004/07/someName";
declare namespace a="http://schemas.datacontract.org/2004/07/someAnotherName";
/LocationCopied/Location/a:Name').value('.', 'nvarchar(100)') ;

select @uName

set @EventBodyXml.modify('
declare default element namespace "http://schemas.datacontract.org/2004/07/someName";
declare namespace a="http://schemas.datacontract.org/2004/07/someAnotherName";
replace value of (/LocationCopied/Location/a:Name/text())[1]
with sql:variable("@uName")
');

print cast(@EventBodyXml as nvarchar(max))
declare @EventBodyXml xml
set @EventBodyXml ='
                <LocationCopied  xmlns="http://schemas.datacontract.org/2004/07/someName" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
                <Location xmlns:a="http://schemas.datacontract.org/2004/07/someAnotherName">
                <a:Name>XXXXXXXXXXXX</a:Name></Location>
                </LocationCopied>'


declare @uName nvarchar(100)
set @uName='yyyyyyyyyyyyy'


set @EventBodyXml.modify('
  declare default element namespace "http://schemas.datacontract.org/2004/07/someName";
  declare namespace a="http://schemas.datacontract.org/2004/07/someAnotherName";
  replace value of (/LocationCopied/Location/a:Name/text())[1]
  with sql:variable("@uName")
');

print cast(@EventBodyXml as nvarchar(max))