Skip to content

Instantly share code, notes, and snippets.

@jwoschitz
Created August 12, 2011 16:55
Show Gist options
  • Save jwoschitz/1142462 to your computer and use it in GitHub Desktop.
Save jwoschitz/1142462 to your computer and use it in GitHub Desktop.
Parse XML in T-SQL
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