Modify sql server xml element name variable in xquery
sql, xml, xquery
Solution
You can use `local-name()` in a predicate to find the node you want to modify.
declare @var2 varchar(50) = 'ElementName'
declare @var varchar(50) = 'false'
update CURVES
set CURVEENTITY.modify('replace value of ((*[local-name() = sql:variable("@var2")]/text())[1])
with sql:variable("@var")')
where ID = 3
Problem
I need the name of the element is also a variable (is a parameter of the procedure) Instead of this, which works fine: ``` DECLARE @VAR VARCHAR(5) SET @VAR = 'false' UPDATE CURVES SET CURVEENTITY.modify( 'replace value of (/ElementName/text())[1] with sql:variable("@VAR")') WHERE ID = 3 ``` But I would like something like this: ``` DECLARE @VAR VARCHAR(5) DECLARE @VAR2 VARCHAR(20) SET @VAR = 'false' SET @VAR2 = 'ElementName' UPDATE CURVES SET CURVEENTITY.modify( 'replace value of (/sql:variable("@VAR2")/text())[1] with sql:variable("@VAR")') WHERE ID = 3 ``` But it does not work! How can I do this?