Solved

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

Posted on 2010-08-18
4
896 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 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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Problem with SqlConnection 4 160
Service Statictic 11 30
Excel conversion issue with Sql server 14 47
email the result out from a T-SQL queries 29 63
This is basically a blog post I wrote recently. I've found that SARGability is poorly understood, and since many people don't read blogs, I figured I'd post it here as an article. SARGable is an adjective in SQL that means that an item can be fou…
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

920 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now