"我有一个xml字段,其中包含以下值:
<table>
<id>{cdf8cafc-2b3c-454b-aab8-c6c27e96b675}</id>
<rows>
<row>
<columns>
<column name="po_desc" value="Item-1" type="System.String" />
<column name="Qty" value="15" type="System.Int32" />
<column name="price" value="100" type="System.Decimal" />
<column name="linetotal" value="1500" type="System.Decimal" />
<column name="DefaultKey" value="1" type="System.Int32" />
</columns>
</row>
<row>
<columns>
<column name="po_desc" value="" type="System.String" />
<column name="Qty" value="1" type="System.Int32" />
<column name="price" value="1" type="System.Decimal" />
<column name="linetotal" value="1" type="System.Decimal" />
<column name="DefaultKey" value="2" type="System.Int32" />
</columns>
</row>
</rows>
</table>
我需要将其转换为行和列,如下所示
PO_desc Qty Price LineTotal
=====================================
item-1 15 100 1500
Item-2 1 1 1
=======================================
使用MS SQL查询。
答案 0 :(得分:0)
尝试这样
DECLARE @DummyTable TABLE(ID INT IDENTITY, YourXmlColumn XML);
INSERT INTO @DummyTable VALUES
('<table>
<id>{cdf8cafc-2b3c-454b-aab8-c6c27e96b675}</id>
<rows>
<row>
<columns>
<column name="po_desc" value="Item-1" type="System.String" />
<column name="Qty" value="15" type="System.Int32" />
<column name="price" value="100" type="System.Decimal" />
<column name="linetotal" value="1500" type="System.Decimal" />
<column name="DefaultKey" value="1" type="System.Int32" />
</columns>
</row>
<row>
<columns>
<column name="po_desc" value="" type="System.String" />
<column name="Qty" value="1" type="System.Int32" />
<column name="price" value="1" type="System.Decimal" />
<column name="linetotal" value="1" type="System.Decimal" />
<column name="DefaultKey" value="2" type="System.Int32" />
</columns>
</row>
</rows>
</table>');
这是查询
SELECT r.value('(columns/column[@name="po_desc"]/@value)[1]','nvarchar(max)') AS PO_Desc
,r.value('(columns/column[@name="Qty"]/@value)[1]','int') AS Qty
,r.value('(columns/column[@name="price"]/@value)[1]','decimal') AS Price
,r.value('(columns/column[@name="linetotal"]/@value)[1]','decimal') AS LineTotal
FROM @DummyTable AS t
CROSS APPLY t.YourXmlColumn.nodes('/table/rows/row') AS A(r);