Created
August 12, 2011 16:55
-
-
Save jwoschitz/1142462 to your computer and use it in GitHub Desktop.
Parse XML in T-SQL
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
DECLARE @XML XML | |
SET @XML ='<Facility> | |
<Resources> | |
<Resource Type="Printer"> | |
<Item Id="3" Value="Bubbleshots Deskjet Pro" /> | |
<Item Id="253" Value="Topware LaserPrinter 5000" /> | |
<Item Id="7" Value="Jabberbox ClassyImage" /> | |
<Item Id="89" Value="Photoopia Megastar Over9000" /> | |
</Resource> | |
<Resource Type="Phone"> | |
<Item Id="24" Value="Bananaphone L37" /> | |
</Resource> | |
</Resources> | |
</Facility>' | |
SELECT | |
Id = item.value('(@Id)', 'INT'), | |
Value = item.value('(@Value)', 'NVARCHAR(128)') | |
FROM @XML.nodes('//Facility/Resources/Resource/Item') AS Facility(item) | |
WHERE item.value('(../@Type)', 'NVARCHAR(128)') = 'Printer' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment