0

I would like to know, if anyone can help how to get NODE name o XML variable using TSQL.

<ROOT>
<IDS>
<ID>
<NAME>bla1</NAME>
<AGE>25</AGE>
</ID>
<ID>
<NAME>bla2</NAME>
<AGE>26</AGE>
</ID>
</IDS>
</ROOT>

After my query, I should be able to get the nodes names: NAME, AGE

My SQL server is MSSQL 2005.

1 Answer 1

1

This would give you the node names for the children of the first ID node:

DECLARE @x xml

SET @x = '<ROOT>
<IDS>
<ID>
<NAME>bla1</NAME>
<AGE>25</AGE>
</ID>
<ID>
<NAME>bla2</NAME>
<AGE>26</AGE>
</ID>
</IDS>
</ROOT>'

SELECT T.c.value('local-name(.)', 'varchar(50)') 
FROM @x.nodes('/ROOT/IDS/ID[1]/*') T(c)
Sign up to request clarification or add additional context in comments.

Comments

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.