-2

我有一个 xml 数据类型的列,如下所示:

       <Test ID="rs232lon">
        <Param Name="corel" Value="0.00000" />
        <Param Name="co2rel" Value="10.8000" />
        <Param Name="hcrel" Value="111.000" />
        <Param Name="o2rel" Value="0.95000" />
      </Test>

我怎么能得到这样的 TSQL 的结果:

corel       co2rel        hcrel        o2rel    
------------------------------------------          
0.00000     10.8000       111.000     0.95000       
4

1 回答 1

1
DECLARE @CocoJambo TABLE (
    ID INT IDENTITY PRIMARY KEY,
    XmlColumn XML NOT NULL
);

INSERT  @CocoJambo (XmlColumn)
VALUES  (N'<Test ID="rs232lon">
    <Param Name="corel" Value="0.00000" />
    <Param Name="co2rel" Value="10.8000" />
    <Param Name="hcrel" Value="111.000" />
    <Param Name="o2rel" Value="0.95000" />
  </Test>');

SELECT  a.*,
        b.c.value('(Param[@Name="corel"]/@Value)[1]','NUMERIC(38,5)') AS corel,
        b.c.value('(Param[@Name="co2rel"]/@Value)[1]','NUMERIC(38,5)') AS co2rel,
        b.c.value('(Param[@Name="hcrel"]/@Value)[1]','NUMERIC(38,5)') AS hcrel,
        b.c.value('(Param[@Name="o2rel"]/@Value)[1]','NUMERIC(38,5)') AS o2rel
FROM    @CocoJambo a
CROSS APPLY a.XmlColumn.nodes('/Test') AS b(c)

结果:

ID XmlColumn                corel   co2rel   hcrel     o2rel
-- ------------------------ ------- -------- --------- -------
1  <Test ID="rs232lon">...  0.00000 10.80000 111.00000 0.95000
于 2013-01-19T20:34:41.310 回答