Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2010-08-18
4
Medium Priority
?
913 Views
Last Modified: 2012-05-10
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'.

0
Comment
Question by:sureshbabukrish
  • 2
4 Comments
 
LVL 58

Accepted Solution

by:
cyberkiwi earned 2000 total points
ID: 33481325

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

Open in new window

0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 33481456
Nice one!  A good example of creating dynamic attributes without using Dynamic SQL, if ever I saw one.  :)
0
 
LVL 58

Expert Comment

by:cyberkiwi
ID: 33481491
And it was only weeks ago that I was schooled on how to take sql:variable into xquery .. queries.
0
 
LVL 9

Author Closing Comment

by:sureshbabukrish
ID: 33482046
it answer for the question i asked.
0

Featured Post

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…

971 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question