I have an XMLData table. In your Data column I have data like the following:
<?xml version="1.0" encoding="utf-16"?>
<table>
<id>{6ebd0af9-9838-47f5-8aad-45cf9d4201f7}</id>
<rows>
<row>
<columns>
<column name="FechaDev" value="01-August-2017" type="System.DateTime" />
<column name="Importe" value="100" type="System.Decimal" />
<column name="DefaultKey" value="1" type="System.Int32" />
</columns>
</row>
<row>
<columns>
<column name="FechaDev" value="01-September-2017" type="System.DateTime" />
<column name="Importe" value="200" type="System.Decimal" />
<column name="DefaultKey" value="2" type="System.Int32" />
</columns>
</row>
<row>
<columns>
<column name="FechaDev" value="01-October-2017" type="System.DateTime" />
<column name="Importe" value="300" type="System.Decimal" />
<column name="DefaultKey" value="3" type="System.Int32" />
</columns>
</row>
</rows>
<key>DefaultKey</key>
<total>0</total>
<AddOnKey>0</AddOnKey>
<data />
<parameters />
</table>
I need to make a query that returns me for example, the amounts that correspond to dates within the month of August, but already to simply list the data I see myself in trouble.
I have tried the following:
select x.ID,
tabla.columna.value('@FechaDev', 'datetime') as fecha,
tabla.columna.value('@Importe', 'varchar(64)') as importe
from XMLData x
outer apply x.data.nodes('?xml/table/rows/row/columns') as tabla(columna)
But it returns the following error, because it seems that the system does not identify well what is x.data.nodes ...
Mens. 9506, Nivel 16, Estado 1, Línea 5
The XMLDT method 'nodes' can only be invoked on columns of type xml.
I've also tried this:
DECLARE @xml XML
SELECT @xml = Data FROM XMLData
SELECT
Tabla.Columna.value('FechaDev', 'datetime') AS Fecha,
Tabla.Columna.value('Importe', 'VARCHAR(64)') AS Importe
FROM @xml.nodes('//column') Tabla(Columna)
But it does not work either, it gives me the following error:
Mens. 2389, Nivel 16, Estado 1, Línea 16
XQuery [value()]: 'value()' requires a singleton (or empty sequence), found operand of type 'xdt:untypedAtomic *'
Obviously, something I'm not doing well, any help will be very grateful.