Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8288083
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T12:12:27+00:00 2026-06-08T12:12:27+00:00

Imagine I have some XML that looks like this: DECLARE @xml XML SET @xml

  • 0

Imagine I have some XML that looks like this:

DECLARE @xml XML
SET @xml = CAST(N'<row><id>1</id><a>b</a></row><row><id>2</id><a>x</a></row>' AS XML)

That is:

<row>
    <id>1</id>
    <a>b</a>
</row>
<row>
    <id>2</id>
    <a>x</a>
</row>

The id element is the key of each row, and it may be dynamic, so I have it stored in a variable:

DECLARE @idname NVARCHAR(MAX)
SET @idname = N'id'

I would like to have a resultset with two columns, the id value and the row’s contents.
I tried this:

SELECT node.value(N'xxx', N'NVARCHAR(MAX)') AS key, CAST(node.query(N'.') AS NVARCHAR(MAX)) AS value
FROM @xml.nodes(N'/row') AS roots(node)

The problem is, I don’t know what to put inside value, instead of xxx. I would like to be able to do something like:

node.value(@idname, N'VARCHAR(MAX))

So that my variable would be evaluated as an XPath expression, but I know it’s not possible. If I use instead:

node.value(N'sql:variable("@idname")', N'NVARCHAR(MAX)')

It will return the value that’s in the @idname variable, instead of evaluating it.

What I want to have is:

--------------------------------------

key           |value

--------------------------------------

1             |<id>1</id><a>b</a>

2             |<id>2</id><a>x</a>

So, is this possible at all?

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T12:12:27+00:00Added an answer on June 8, 2026 at 12:12 pm
    select T.N.value('(*[local-name(.)=sql:variable("@idname")])[1]', 'int') as [key],
           T.N.query('*') as value
    from @xml.nodes('/row') as T(N)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a csv file that contains XML entries. Imagine that each XML entry
Imagine you have an entity that has some relations with other entities and you
Imagine you have a library for working with some sort of XML file or
Assume I have some program that pulls airline data. Each airline data uses a
I have an iOS app that is calling a web service for some XML
Imagine you have an app that manages some library and has its own data.
I have an iOS 5 app that uses NSURLConnection to load some XML via
I have an XML containing lots of information, but there are some informations that
Let's imagine I have a bash script, where I call this: bash -c some_command
Imagine you have two views with code like the following: controller_a/a.html.erb <%= content_tag(:div) do

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.