If I get this correctly you are getting parameters within an XML as key-value-pairs. What you try to achieve is to read the value into the fitting variable. Try it like this:
DECLARE @xml XML;
SET @xml='<root><name>John </name><age>8</age></root>';
DECLARE @name NVARCHAR(100);
DECLARE @age INT;
SET @[email protected]('(/root/name)[1]','nvarchar(max)');
SET @age [email protected]('(/root/age)[1]','int');
SELECT @name,@age;
If you'd need to query the values from a variable name, you can use this:
DECLARE @nodeName NVARCHAR(100)='name';
SET @[email protected]('(/root/*[local-name()=sql:variable("@nodeName")])[1]','nvarchar(max)');
The central idea is to use an XQuery-expression (/root/*[local-name()=sql:variable("@nodeName")])[1] to pick the first node's value, where the element's name is like the given parameter
<name>and<age>in several strings, by the respective element values in an XML stored in a variable... Correct? Or, do you want to get the element values of any element in the XML, and substitute any occurance of those tags in those strings?