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 )
Produce:
FilterID Name Code Department Number
----------- ---------- ---------- ---------- -----------
1219 Fred 510 N 305327
3578 Gary 001 B 692690
3579 George 001 X 35933
Nota:il [1]
è necessario per indicare che si desidera selezionare il primo valore della sequenza poiché la query potrebbe restituire più di un valore corrispondente per riga (immagina il tuo XML contenente diversi FilterID per SpangemansFilter).
Ho pensato che fosse utile sapere, quindi ho cercato su Google e letto molti post fino a quando non ho trovato questo.
AGGIORNAMENTO Per caricare da file:
DECLARE @xmlData XML
SET @xmlData = (
SELECT * FROM OPENROWSET (
BULK 'C:\yourfile.xml', SINGLE_CLOB
) AS xmlData
)
SELEZIONA @xmlData