如何阅读xml列?

时间:2015-10-06 10:07:03

标签: sql sql-server xml

我在customer表中有一个xml脚本。下面的结构是表格中我需要的结构



<pricer xmlns="http://www.Card.com/xxxx/xxxx">
  <service>
    <response>
      <businessObjectModel>
        <Id>0</Id>
        <Card xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="Card">
          <ID>1</ID>
          <Inc>0.0</Inc>
          <value>0.0</value>
          <Tvalue1>0.0</Tvalue1>
        </Card>
      </businessObjectModel>
&#13;
&#13;
&#13;

我需要如下表所示:

enter image description here

1 个答案:

答案 0 :(得分:1)

如果你想迭代卡片,你可以这样做:

   DECLARE @XML XML
SET @XML = '
<pricer xmlns:xsi="http://www.Card.com/xxxx/xxxx">
  <service>
    <response>
      <businessObjectModel>
        <Id>0</Id>
        <Card xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="Card">
          <ID>1</ID>
          <Inc>0.0</Inc>
          <value>0.0</value>
          <Tvalue1>0.0</Tvalue1>
        </Card>
      </businessObjectModel>
      <businessObjectModel>
        <Id>1</Id>
        <Card xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="Card">
          <ID>1</ID>
          <Inc>2.0</Inc>
          <value>1.0</value>
          <Tvalue1>2.0</Tvalue1>
        </Card>
      </businessObjectModel>
      </response></service></pricer>'

SELECT   
Tbl.Col.value('ID[1]', 'int'),
Tbl.Col.value('Inc[1]', 'decimal'),
Tbl.Col.value('value[1]', 'decimal'),
Tbl.Col.value('Tvalue1[1]', 'decimal')
FROM   @XML.nodes('pricer/service/response/businessObjectModel/Card') Tbl(Col)
GO