Question : how to pass dynamic value to root node attribute of XML using Xquery?

Code Snippet:


DECLARE @TempTable TABLE  
  (  
      _tagName VARCHAR(25),
    _value1 VARCHAR(50),  
    _value2 VARCHAR(50)  
  )  

Declare @valuation varchar(25)

SET @valuation = '123'

INSERT @TempTable SELECT 'TEST', 'FScore', '123456789'  
INSERT @TempTable SELECT 'TEST', 'EstimatedMarketingTime', '123512'

SELECT (SELECT
      _value1 AS [_Name],  
    _value2 AS [_Value],
      @valuation
FROM @TempTable AS A WHERE _TagName = 'TEST'                  
FOR XML PATH('TEST'),      TYPE).query('  
      <TEST PropertyAppraisedValueAmount="Need to pass Variable Value">  
      {  
            for $TEST in /TEST  
            return
               <Key _Name="{data($TEST/_Name)}" _Value="{data($TEST/_Value)}"></Key>
      }
      </TEST>')
GO

Expected:
In the tag <TEST PropertyAppraisedValueAmount="Need to pass Variable Value">, "Need to pass Variable Value" has to be replaced with the variable @valuation.
Note:
The @valuation will be dynamic in the real scenario but for sample I have hard coded the value to'123'.

Answer : how to pass dynamic value to root node attribute of XML using Xquery?

1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
DECLARE @TempTable TABLE  
  (  
      _tagName VARCHAR(25),
    _value1 VARCHAR(50),  
    _value2 VARCHAR(50)  
  )  

Declare @valuation varchar(25)

SET @valuation = '123'

INSERT @TempTable SELECT 'TEST', 'FScore', '123456789'  
INSERT @TempTable SELECT 'TEST', 'EstimatedMarketingTime', '123512'

SELECT (SELECT
      _value1 AS [_Name],  
    _value2 AS [_Value],
      @valuation
FROM @TempTable AS A WHERE _TagName = 'TEST'                  
FOR XML PATH('TEST'), TYPE).query('
      <TEST PropertyAppraisedValueAmount="{ sql:variable("@valuation") }">
      {  
            for $TEST in /TEST  
            return
               <Key _Name="{data($TEST/_Name)}" _Value="{data($TEST/_Value)}"></Key>
      }
      </TEST>')
GO
Random Solutions  
 
programming4us programming4us