Can't modify XML node's value in T-SQL (ms sql)
Question
I would like to modify my xml's node value in mssql, but my approach doesn't seem to work.
Here is an xml sample:
<xml>
<ProjectManager>
<People>
<DisplayNames>John Smith</DisplayNames>
<LoginNames>ABC\jsmith</LoginNames>
</People>
</ProjectManager>
</xml>
The part of my function:
CREATE FUNCTION [dbo].[MyFunc](
@properties xml,
@key nvarchar(50),
@newvalue nvarchar(max),
@datatype nvarchar(50) = null,
@node nvarchar(50) = null)
RETURNS xml
WITH SCHEMABINDING
AS BEGIN
DECLARE @temp XML = @properties
IF LOWER(@datatype) = 'people'
SET @temp.modify('replace value of (/xml/*[local-name() = sql:variable("@key")][1]/People/*[local-name() = sql:variable("@node")]/text())[1] with sql:variable("@newvalue")')
RETURN @temp;
END
Calling the function:
set @result = dbo.MyFunc(@myXML,'ProjectManager','Somebody','People','DisplayName')
Solution
DisplayName is plural in your XML.
Use
set @result = dbo.MyFunc(@myXML,'ProjectManager','Somebody','People','DisplayNames')
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow