Skip to content

Instantly share code, notes, and snippets.

@shammelburg
Last active May 11, 2016 12:04
Show Gist options
  • Save shammelburg/ab2669078680515f7cab4a336b837eb1 to your computer and use it in GitHub Desktop.
Save shammelburg/ab2669078680515f7cab4a336b837eb1 to your computer and use it in GitHub Desktop.
-- xml retrieval
declare @xml xml
SET @xml = '<Categories>
<Category ID="4">
<name>Accessories</name>
</Category>
<Category ID="1">
<name>Bikes</name>
</Category>
<Category ID="3">
<name>Clothing</name>
</Category>
<Category ID="2">
<name>Components</name>
</Category>
<Category ID="5">
<name>Something</name>
</Category>
</Categories>'
-- Option 1
declare @documentHandle int
exec sp_xml_preparedocument @documentHandle out, @xml
select * from openxml(@documentHandle, '/Categories/Category',1)
with(
Name varchar(50) 'name',
CategoryID int '@ID'
)
-- Option 2
SELECT script.Item.value('@ID','int') FirstName,
script.Item.query('./name').value('.','VARCHAR(250)') Name
FROM @xml.nodes('/Categories/Category') AS script(Item)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment