I have a table, let's call it TBL
, with a column of type XML
.
The XML
column (called xml
here) is of the format:
<START xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="87jjhanM">
<Header xmlns="">
...
</Header>
<Fetch xmlns="">
.....
</Fetch>
<Send xmlns="">
<Supplier>
<Deals>
<Deal>
<Field1> </Field1>
<Field2> </Field2>
</Deal>
</Deals>
</Supplier>
<Supplier>
<Deals>
<Deal>
<Field1> </Field1>
<Field2> </Field2>
</Deal>
</Deals>
</Supplier>
</Send>
</START>
Notice that each XML document can have multiple <Supplier>
tags. What I am interested in, is getting the values of Field1
and Field2
.
From reading e.g How can I query a SQL Server XML column and return all values for a specific node? and Getting multiple records from xml column with value() in SQL Server it seems that I should be using some sort of cross apply.
I just can't seem to make it work. With value()
, I am perfectly capable of getting the first instance found, but with nodes()
I fail miserably (SQL Server).
My blatant rip-off attempt (here only for Field1) for getting all the Field1 values are:
SELECT
xml.value('(/Supplier/Deals/Deal/Field1[1])[1]', 'VARCHAR(100)') AS A
FROM
TBL
CROSS APPLY
xml.nodes('/Start/Send') x(A);
which, as far as I understand, simply looks for the first value /Supplier/Deals/Deal/Field1
under each /Start/Send/
, but it does not work - it simply returns 0 rows affected (I know the specific structure exist in the XML files I should say).
I am sure this is quite easy, but I just can't wrap my head around why the above does not work. Any help is greatly appreciated.