我有一个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
答案 0 :(得分: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