如何将XML列中的数据提取到自己的列中?

时间:2011-09-29 15:13:13

标签: sql xml

我有280,000行,其中包含一个名为body的列。此列包含xml数据。

下面显示了一个列值的副本。我需要将XML字段esntimestamp_utcLatitudeLongitudetriggers的元素中包含的数据提取到新列中,列名与元素名称相同。

我过去曾为此做过工作,但放弃了。我不需要弄清楚如何去做。请给我任何你可能有的建议。

谢谢!!!

<?xml version="1.0" encoding="UTF-8"?>
<telemetry>
   <esn>1127877</esn>
   <timestamp_utc>5/28/2011 9:18:30 PM</timestamp_utc>
   <latitude>59.023672</latitude>
   <longitude>-118.836408</longitude>
   <ignition>N</ignition>
   <geofence>0</geofence>
   <speed>0</speed>
   <heading>0</heading>
   <milestraveled>0.00</milestraveled>
   <triggers>Contact_State_Change, IO_1_Closed, No_Vibration_Detected, </triggers>
   <miscellaneous>Standard3</miscellaneous>
   <contact1counts>0</contact1counts>
   <contact2counts>0</contact2counts>
   <io1state>0</io1state>
   <io2state>0</io2state>
</telemetry>

1 个答案:

答案 0 :(得分:2)

您可以使用以下内容:

SELECT  
   ID,
   body.value('(telemetry/esn)[1]', 'bigint') AS 'esn',
   body.value('(telemetry/timestamp_utc)[1]', 'varchar(50)') AS 'esn',
   body.value('(telemetry/longitude)[1]', 'float') AS 'longitude',
   body.value('(telemetry/latitude)[1]', 'float') AS 'latitude',             
   body.value('(telemetry/timestamp_utc)[1]', 'varchar(50)') AS 'triggers'
FROM
   dbo.YourTableNameHere

这是否有效,并为您提供正确的数据?