MOCKSTACKS
EN
Questions And Answers

More Tutorials









SQLServer XML

Query from XML Data Type


DECLARE @xmlIN XML = '<TableData>
<aaa Main="First">
 <row name="a" value="1" />
 <row name="b" value="2" />
 <row name="c" value="3" />
</aaa>
<aaa Main="Second">
 <row name="a" value="3" />
 <row name="b" value="4" />
 <row name="c" value="5" />
</aaa>
<aaa Main="Third">
 <row name="a" value="10" />
 <row name="b" value="20" />
 <row name="c" value="30" />
</aaa>
</TableData>'
SELECT t.col.value('../@Main', 'varchar(10)') [Header],
t.col.value('@name', 'VARCHAR(25)') [name],
t.col.value('@value', 'VARCHAR(25)') [Value]
FROM @xmlIn.nodes('//TableData/aaa/row') AS t (col)

Results

Header name Value
First a 1
First b 2
First c 3
Second a 3
Second b 4
Second c 5
Third a 10
Third b 20
Third c 30


Conclusion

In this page (written and validated by ) you learned about SQLServer XML . What's Next? If you are interested in completing SQLServer tutorial, your next topic will be learning about: SQLServer Primary Keys.



Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue: mockstacks@gmail.com.


Share On:


Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using Mockstacks.com, you agree to have read and accepted our terms of use, cookies and privacy policy.