Search

Tuesday, March 6, 2012

select from xml using nodes() function

Check below script:


DECLARE @myXml AS XML
SET @myXml = 
'<ArrayOfCustomer>
        <Customer id="1">
                <Description>Mike</Description>
        </Customer>
        <Customer id="2">
                <Description>James</Description>
        </Customer>      
        <Customer id="3">
                <Description>Brian</Description>
        </Customer>      
        <Customer id="4">
                <Description>Taylor</Description>
        </Customer>      
</ArrayOfCustomer>'


SELECT
        T.c.value('@id[1]', 'int') AS [id]
        , T.c.value('Description[1]', 'nvarchar(max)') AS [description]
FROM @myXml.nodes('/ArrayOfCustomer/Customer') AS T(c)

No comments:

Post a Comment