set @xmlData='<?xml version="1.0"?>
<ArrayOfSpangemansFilter xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<SpangemansFilter>
<FilterID>1219</FilterID>
<Name>Fred</Name>
<Code>510</Code>
<Department>N</Department>
<Number>305327</Number>
</SpangemansFilter>
<SpangemansFilter>
<FilterID>3578</FilterID>
<Name>Gary</Name>
<Code>001</Code>
<Department>B</Department>
<Number>0692690</Number>
</SpangemansFilter>
<SpangemansFilter>
<FilterID>3579</FilterID>
<Name>George</Name>
<Code>001</Code>
<Department>X</Department>
<Number>35933</Number>
</SpangemansFilter>
</ArrayOfSpangemansFilter>'
SELECT
ref.value('FilterID[1]', 'int') AS FilterID ,
ref.value('Name[1]', 'NVARCHAR (10)') AS Name ,
ref.value('Code[1]', 'NVARCHAR (10)') AS Code ,
ref.value('Department[1]', 'NVARCHAR (3)') AS Department,
ref.value('Number[1]', 'int') AS Number
FROM @xmlData.nodes('/ArrayOfSpangemansFilter/SpangemansFilter')
xmlData( ref )
Produceert:
FilterID Name Code Department Number
----------- ---------- ---------- ---------- -----------
1219 Fred 510 N 305327
3578 Gary 001 B 692690
3579 George 001 X 35933
Opmerking:de [1]
is nodig om aan te geven dat u de eerste waarde van de reeks wilt selecteren, aangezien de query meer dan één overeenkomende waarde per rij kan retourneren (stel u voor dat uw XML meerdere FilterID's per SpangemansFilter bevat).
Ik dacht dat dit nuttig was om te weten, dus ik Googlede en las veel berichten totdat ik deze.
UPDATE Om uit bestand te laden:
DECLARE @xmlData XML
SET @xmlData = (
SELECT * FROM OPENROWSET (
BULK 'C:\yourfile.xml', SINGLE_CLOB
) AS xmlData
)
SELECT @xmlData