-- variables DECLARE @xml XML = ' <dinosaurs> <d name="Aachenosaurus" url="http://en.wikipedia.org/wiki/Aachenosaurus" /> <d name="Aardonyx" url="http://en.wikipedia.org/wiki/Aardonyx" /> <d name="Abdallahsaurus" url="http://en.wikipedia.org/wiki/Abdallahsaurus" /> <d name="Abelisaurus" url="http://en.wikipedia.org/wiki/Abelisaurus" /> <d name="Abrictosaurus" url="http://en.wikipedia.org/wiki/Abrictosaurus" /> <d name="Abrosaurus" url="http://en.wikipedia.org/wiki/Abrosaurus" /> <d name="Abydosaurus" url="http://en.wikipedia.org/wiki/Abydosaurus" /> <d name="Acanthopholis" url="http://en.wikipedia.org/wiki/Acanthopholis" /> <d name="Achelousaurus" url="http://en.wikipedia.org/wiki/Achelousaurus" /> <d name="Achillesaurus" url="http://en.wikipedia.org/wiki/Achillesaurus" /> </dinosaurs>' -- use common table expression to shred data ;WITH ShreddedData (name, url) AS ( SELECT x.node.value('@name', 'VARCHAR(255)'), x.node.value('@url', 'VARCHAR(255)') FROM @xml.nodes('dinosaurs/d') x(node) ) SELECT * FROM ShreddedData;
Shredding XML data (attributes) using SQL Server
Here's a simple example of shredding XML data (attributes) in SQL Server.
0 comments: