我正在使用sql server, 在我的表格中,我有一个" ntext" column.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查询来做到这一点.. 提前谢谢......
答案 0 :(得分:0)
见下文:
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/en-us/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