Solved

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

Posted on 2010-08-18
4
903 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 58

Accepted Solution

by:
cyberkiwi earned 500 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

Hi all, It is important and often overlooked to understand “Database properties”. Often we see questions about "log files" or "where is the database" and one of the easiest ways to get general information about your database is to use “Database p…
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…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

752 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