[英]How to get the data from “ntext” column in sql server
我正在使用sql server,在我的表中有一个“ ntext”列。ntext列中的数据如下
"<?xml version="1.0" encoding="UTF-8"?>
<processEngine id="5000001" instanceName="bg-claritysql.excers">
<controller heartBeat="2014-11-14T19:35:57"/>
<loader heartBeat="2014-11-14T19:35:57" queueLength="1"/>
<conditionWaitList queueLength="1"/>
<retryWaitList queueLength="0"/>
<actionWaitList queueLength="0"/>
<PreConditionPipelineManager load="3.451246679588729E-7" noOfPipelines="2" queueLength="0"
recentLoad="8.510423949066926E-6">
<pipeline heartBeat="2014-10-11T04:14:17" index="1" load="1.0022981644497761E-9"
name="Pre Condition Pipeline 1"
recentLoad="1.0022981684681825E-9"
runTime="3"
running="false"
startTime="2014-10-11T04:14:17"/>
</PreConditionPipelineManager>
<PostConditionTransitionPipelineManager load="8.273414600907745E-7" noOfPipelines="3" queueLength="0"
</processEngine>"
我想获取heartBeat,name,recentLoad,runTime,running,startTime值。 谁能帮助我如何使用sql查询来做到这一点。.在此先感谢......
见下文:
declare @str nvarchar(max) = '<processEngine id="5000001" instanceName="bg-claritysql.excers">
<controller heartBeat="2014-11-14T19:35:57"/>
<loader heartBeat="2014-11-14T19:35:57" queueLength="1"/>
<conditionWaitList queueLength="1"/>
<retryWaitList queueLength="0"/>
<actionWaitList queueLength="0"/>
<PreConditionPipelineManager load="3.451246679588729E-7" noOfPipelines="2" queueLength="0" recentLoad="8.510423949066926E-6">
<pipeline heartBeat="2014-10-11T04:14:17" index="1" load="1.0022981644497761E-9"
name="Pre Condition Pipeline 1"
recentLoad="1.0022981684681825E-9"
runTime="3"
running="false"
startTime="2014-10-11T04:14:17"/>
<pipeline heartBeat="2014-10-11T04:14:17" index="1" load="1.0022981644497761E-9"
name="Pre Condition Pipeline 1"
recentLoad="1.0022981684681825E-9"
runTime="3"
running="false"
startTime="2014-10-11T04:14:17"/>
</PreConditionPipelineManager>
<PostConditionTransitionPipelineManager load="8.273414600907745E-7" noOfPipelines="3" queueLength="0" />
</processEngine>'
declare @xml xml = @str
select
t.value('(./controller/@heartBeat)[1]', 'datetime') as HeartBeat,
t.value('(./PreConditionPipelineManager/@recentLoad)[1]', 'float') as RecentLoad,
t.value('(./PreConditionPipelineManager/pipeline/@recentLoad)[2]', 'float') as PipelineRecentLoad,
t.value('(./PreConditionPipelineManager/pipeline/@running)[1]', 'bit') as PipelineRunning,
t.value('(./PreConditionPipelineManager/pipeline/@startTime)[1]', 'datetime') as PipelineStartTime
from
@xml.nodes('processEngine') as a(t)
您可以使用任何XQuery表达式。 有关更多详细信息,请参见http://msdn.microsoft.com/zh-cn/library/ms189075.aspx 。
或者,您可以将表列更新为xml
类型而不是ntext
并使用类似以下的查询:
select
xmlcolumn.value('(./processEngine/controller/@heartBeat)[1]', 'datetime') as HeartBeat,
xmlcolumn.value('(./processEngine/PreConditionPipelineManager/@recentLoad)[1]', 'float') as RecentLoad,
xmlcolumn.value('(./processEngine/PreConditionPipelineManager/pipeline/@recentLoad)[2]', 'float') as PipelineRecentLoad,
xmlcolumn.value('(./processEngine/PreConditionPipelineManager/pipeline/@running)[1]', 'bit') as PipelineRunning,
xmlcolumn.value('(./processEngine/PreConditionPipelineManager/pipeline/@startTime)[1]', 'datetime') as PipelineStartTime
from
xmltable
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.