1

Given this SQL:

DECLARE @content XML
SET @content =
'<people>
  <person id="1" bimble="1">
    <firstname bobble="gomble">John</firstname>
    <surname>Doe</surname>
  </person>
  <person id="2" bimble="11">
    <firstname bobble="zoom">Mary</firstname>
    <surname>Jane</surname>
  </person>
  <person id="4" bimble="10">
    <firstname bobble="womble">Matt</firstname>
    <surname>Spanner</surname>
  </person>
</people>'

I want to retrieve every attribute, it's value and parent element's name as a table:

Parent Name Attribute Name Attribute Value
----------- -------------- ---------------
person      id             1
person      bimble         1
firstname   bobble         gomble
person      id             2
person      bimble         11
firstname   bobble         zoom
person      id             4
person      bimble         10
firstname   bobble         womble

1 Answer 1

6

My initial answer (to my own question):

SELECT
    elem.value('local-name(..)', 'nvarchar(10)') AS 'Parent Name',
    elem.value('local-name(.)', 'nvarchar(10)') AS 'Attribute Name',
    elem.value('.', 'nvarchar(10)') AS 'Attribute Value'
FROM
    @content.nodes('//@*') AS El(elem)
Sign up to request clarification or add additional context in comments.

2 Comments

Why not add this at the bottom the question?
Because it's an answer and the system marks you down for not having a selected answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.